1ចងក្រងដោយ: ថនសារិ
រំលឹរដេដរៀនSpring Framworkក្រលងឆមាសដលើរទី២ឆ្ន ាំទី៤ររិញ្ញា រ័ក្រព័រមានវិទា
I. Multiple Choice Question
1. Which are the modules of core container?
A. Beans, Core, Context, SpEL
B. Core, Context, ORM, Web
C. Core, Context, Aspects, Test
D. Bean, Core, Context, Test
2. What is a Singleton scope?
A. The scopes the bean definition to a single instance per Spring loC container.
B. The scopes the bean definition to a single instance per HTTP Request.
C. The scopes the bean definition to a single instance per HTTP session.
D. The scopes the bean definition to a single instance per HTTP application/Global session.
3. How to you turn on annotation wiring?
A. Add<annotation-context:config/> to bean configuration
B. Add<annotation-config/> to bean configuration
C. Add<annotation-context-config/> to bean configuration
D. Add<context:annotion-config/> to bean configuration
4. What is a Spring MVC framework?
A. Spring MVC framework is a Model-Value-Class architecture and use to bind model data
with values.
B. The Spring web MVC framework provides model-view-controller architecture and ready
component that can be used to develop flexible and loosely coupled web applications.
C. Spring MVC framework is used for transaction management for web application.
D. Spring MVC framework is used for APO for web application
5. What is ACID in transactional management?
A. Accurate, Controlled, Isolation, Durability
B. Atomicity, Consistency ,Independent, Done
C. Atomicity, Consistency, Isolation, Durability
D. Accurate, Controlled, Independent, Done
6. Can you inject null and empty String values to Spring?
A. Yes
7. What is @Controller annotation?
A. The @Controller annotation indicates that a particular class serves the role of controller.
B. The @Controller annotation indicates how to control the transaction management.
C. The @Controller annotation indicates how to control the dependency injection.
D. The @Controller annotation indicates how to control the aspect Programming.
8. What is request scope?
A. This scopes a bean definition to an HTTP request.
B. This scopes the bean definition to Spring LoC container
C. This scopes the bean definition to HTTP session
D. This scopes the bean definition HTTP Application/ Global session
2ចងក្រងដោយ: ថនសារិ
9. Which of the following is correct about dependency injection?
A. It helps in decoupling application object from each other.
B. It helps in deciding the dependencies of objects.
C. It store objects states in database.
D. It store objects states in files system.
10. What AOP stands for?
A. Aspect Oriented Programming
B. Any Object Programming
C. Asset Oriented Programming
D. Asset Oriented Protocol
3ចងក្រងដោយ: ថនសារិ
II. Answer the Question
1. What is Apache Maven and Gradle?
 Apache Maven is a software project management and comprehension tool. Based on
the concept of a project object model (POM), Maven can manage a project's build,
reporting and documentation from a central piece of information.
 Gradle is an open-source build automation system that builds upon the concepts of
Apache Ant and Apache Maven and introduces a Groovy-based domain-specific
language (DSL) instead of the XML form used by Apache Maven for declaring the
project configuration.
2. What is Web.XML(Web deployment descriptor) use for in spring framework?
 web.xml is a deployment descriptor file while using J2EE. ... Servlet to be accessible
from a browser, then must tell the servlet container what servlets to deploy, and what
URL's to map the servlets to. This is done in the web.xml file of your
Java web application
3. What is <annotation-driven/> tag use for in spring configuration?
 <annotation-driven> is used to detect the annotation like @Controller,
@ResponseBody,….
4. List of 3 type of ViewResolver in spring framework and their usage?
 AbstractCachingViewResolver : Abstract view resolver that caches views. Often views
need preparation before they can be used; extending this view resolver provides
caching.
 XmlViewResolver : Implementation of ViewResolver that accepts a configuration file
written in XML with the same DTD as Spring’s XML bean factories. The default
configuration file is /WEB-INF/views.xml.
 ResourceBundleViewResolver : Implementation of ViewResolver that uses bean
definitions in a ResourceBundle, specified by the bundle base name. Typically you
define the bundle in a properties file, located in the classpath. The default file name
is views.properties.
 UrlBasedViewResolver : Simple implementation of the ViewResolver interface that
effects the direct resolution of logical view names to URLs, without an explicit
mapping definition. This is appropriate if your logical names match the names of your
view resources in a straightforward manner, without the need for arbitrary mappings.
 InternalResourceViewResolver : Convenient subclass of UrlBasedViewResolver that
supports InternalResourceView (in effect, Servlets and JSPs) and subclasses such as
JstlView and TilesView. You can specify the view class for all views generated by this
resolver by using setViewClass(..).
 VelocityViewResolver/FreeMarkerViewResolver : Convenient subclass of
UrlBasedViewResolver that supports VelocityView (in effect, Velocity templates) or
FreeMarkerView ,respectively, and custom subclasses of them.
 ContentNegotiatingViewResolver : Implementation of the ViewResolver interface
that resolves a view based on the request file name or Accept header.
4ចងក្រងដោយ: ថនសារិ
5. Describe annotation bellow with example:
-@Controller
@Controller is similar annotation which mark a class as request handler.
package com.howtodoinjava.web;
@Controller
public class HomeController
{
@GetMapping("/")
public String homeInit(Model model) {
return "home";
}
}
-@Service
@Service is a stereotype for the service layer.
Package com.in28minutes.login
inport org.springframwork.beans.factory.annotation.autowired;
@Controller
public class loginController{
@Autowired
LoginService Service;
@RequestMapping(value="/login",method=RequestMethod.GET)
public String showLoginPage(){
return "login";
}
}
-@Repository
@Repository is a stereotype for persistence layer.
package com.spring.anno;
@Service
public class TestBean
{
public void m1()
{
//business code
}
}
package com.spring.anno;
@Repository
public class TestBean
{
public void update()
{
//persistence code
}
5ចងក្រងដោយ: ថនសារិ
}
-@Rest Controller
@Rest Controller which is a convenience annotation that does nothing more than add the
@Controller and @ResponseBody annotations.
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController
-@Request Mapping
@RequestMapping is one of the most common annotation used in Spring Web applications.
This annotation maps HTTP requests to handler methods of MVC and REST controllers.
@RequestMapping(value = "/ex/foos", method = RequestMethod.GET)
@ResponseBody
public String getFoosBySimplePath() {
return "Get some Foos";
}
-@Path Variable
@PathVariableannotation used to get variable name and its value at controller end. e.g.
@RequestMapping(value = "/ex/foos/{fooid}/bar/{barid}", method = GET)
@ResponseBody
public String getFoosBySimplePathWithPathVariables
(@PathVariable long fooid, @PathVariable long barid) {
return "Get a specific Bar with id=" + barid +
" from a Foo with id=" + fooid;
}
-@RequestParam
The @RequestParam annotation is used with @RequestMapping to bind a web request
parameter to the parameter of the handler method.
The @RequestParam annotation can be used with or without a value. The value specifies
the request param that needs to be mapped to the handler method parameter, as shown
in this code snippet.
The default value of the @RequestParam is used to provide a default value when the
request param is not provided or is empty.
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/fetch", params = {"personId=10"})
6ចងក្រងដោយ: ថនសារិ
String getParams(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = "+id;
}
@RequestMapping(value = "/fetch", params = {"personId=20"})
String getParamsDifferent(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = "+id;
}
}
-@Model Attribute
@ModelAttribute is an annotation that binds a method parameter or method return
value to a named model attribute and then exposes it to a web view.
@ModelAttribute("person")
public Person getPerson(){
return new Person();
}
-@Request Header
@RequestHeader that can be used to map controller parameter to request header value
@Controller
public class HelloController {
@RequestMapping(value = "/hello.htm")
public String hello(@RequestHeader(value="User-Agent") String userAgent)
//..
}
}
-@Request Body
@Request Body is Annotation indicating a method parameter should be bound to the body
of the HTTP request.
@RequestMapping(path = "/something",method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
-@Response Body
The @ResponseBody annotation can be put on a method and indicates that the return
type should be written straight to the HTTP response body (and not placed in a Model, or
interpreted as a view name).
@RequestMapping(path = "/something",method = RequestMethod.PUT)
7ចងក្រងដោយ: ថនសារិ
public @ResponseBody String helloWorld() {
return "Hello World";
}

Spring review_for Semester II of Year 4

  • 1.
    1ចងក្រងដោយ: ថនសារិ រំលឹរដេដរៀនSpring Framworkក្រលងឆមាសដលើរទី២ឆ្នាំទី៤ររិញ្ញា រ័ក្រព័រមានវិទា I. Multiple Choice Question 1. Which are the modules of core container? A. Beans, Core, Context, SpEL B. Core, Context, ORM, Web C. Core, Context, Aspects, Test D. Bean, Core, Context, Test 2. What is a Singleton scope? A. The scopes the bean definition to a single instance per Spring loC container. B. The scopes the bean definition to a single instance per HTTP Request. C. The scopes the bean definition to a single instance per HTTP session. D. The scopes the bean definition to a single instance per HTTP application/Global session. 3. How to you turn on annotation wiring? A. Add<annotation-context:config/> to bean configuration B. Add<annotation-config/> to bean configuration C. Add<annotation-context-config/> to bean configuration D. Add<context:annotion-config/> to bean configuration 4. What is a Spring MVC framework? A. Spring MVC framework is a Model-Value-Class architecture and use to bind model data with values. B. The Spring web MVC framework provides model-view-controller architecture and ready component that can be used to develop flexible and loosely coupled web applications. C. Spring MVC framework is used for transaction management for web application. D. Spring MVC framework is used for APO for web application 5. What is ACID in transactional management? A. Accurate, Controlled, Isolation, Durability B. Atomicity, Consistency ,Independent, Done C. Atomicity, Consistency, Isolation, Durability D. Accurate, Controlled, Independent, Done 6. Can you inject null and empty String values to Spring? A. Yes 7. What is @Controller annotation? A. The @Controller annotation indicates that a particular class serves the role of controller. B. The @Controller annotation indicates how to control the transaction management. C. The @Controller annotation indicates how to control the dependency injection. D. The @Controller annotation indicates how to control the aspect Programming. 8. What is request scope? A. This scopes a bean definition to an HTTP request. B. This scopes the bean definition to Spring LoC container C. This scopes the bean definition to HTTP session D. This scopes the bean definition HTTP Application/ Global session
  • 2.
    2ចងក្រងដោយ: ថនសារិ 9. Whichof the following is correct about dependency injection? A. It helps in decoupling application object from each other. B. It helps in deciding the dependencies of objects. C. It store objects states in database. D. It store objects states in files system. 10. What AOP stands for? A. Aspect Oriented Programming B. Any Object Programming C. Asset Oriented Programming D. Asset Oriented Protocol
  • 3.
    3ចងក្រងដោយ: ថនសារិ II. Answerthe Question 1. What is Apache Maven and Gradle?  Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.  Gradle is an open-source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration. 2. What is Web.XML(Web deployment descriptor) use for in spring framework?  web.xml is a deployment descriptor file while using J2EE. ... Servlet to be accessible from a browser, then must tell the servlet container what servlets to deploy, and what URL's to map the servlets to. This is done in the web.xml file of your Java web application 3. What is <annotation-driven/> tag use for in spring configuration?  <annotation-driven> is used to detect the annotation like @Controller, @ResponseBody,…. 4. List of 3 type of ViewResolver in spring framework and their usage?  AbstractCachingViewResolver : Abstract view resolver that caches views. Often views need preparation before they can be used; extending this view resolver provides caching.  XmlViewResolver : Implementation of ViewResolver that accepts a configuration file written in XML with the same DTD as Spring’s XML bean factories. The default configuration file is /WEB-INF/views.xml.  ResourceBundleViewResolver : Implementation of ViewResolver that uses bean definitions in a ResourceBundle, specified by the bundle base name. Typically you define the bundle in a properties file, located in the classpath. The default file name is views.properties.  UrlBasedViewResolver : Simple implementation of the ViewResolver interface that effects the direct resolution of logical view names to URLs, without an explicit mapping definition. This is appropriate if your logical names match the names of your view resources in a straightforward manner, without the need for arbitrary mappings.  InternalResourceViewResolver : Convenient subclass of UrlBasedViewResolver that supports InternalResourceView (in effect, Servlets and JSPs) and subclasses such as JstlView and TilesView. You can specify the view class for all views generated by this resolver by using setViewClass(..).  VelocityViewResolver/FreeMarkerViewResolver : Convenient subclass of UrlBasedViewResolver that supports VelocityView (in effect, Velocity templates) or FreeMarkerView ,respectively, and custom subclasses of them.  ContentNegotiatingViewResolver : Implementation of the ViewResolver interface that resolves a view based on the request file name or Accept header.
  • 4.
    4ចងក្រងដោយ: ថនសារិ 5. Describeannotation bellow with example: -@Controller @Controller is similar annotation which mark a class as request handler. package com.howtodoinjava.web; @Controller public class HomeController { @GetMapping("/") public String homeInit(Model model) { return "home"; } } -@Service @Service is a stereotype for the service layer. Package com.in28minutes.login inport org.springframwork.beans.factory.annotation.autowired; @Controller public class loginController{ @Autowired LoginService Service; @RequestMapping(value="/login",method=RequestMethod.GET) public String showLoginPage(){ return "login"; } } -@Repository @Repository is a stereotype for persistence layer. package com.spring.anno; @Service public class TestBean { public void m1() { //business code } } package com.spring.anno; @Repository public class TestBean { public void update() { //persistence code }
  • 5.
    5ចងក្រងដោយ: ថនសារិ } -@Rest Controller @RestController which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController -@Request Mapping @RequestMapping is one of the most common annotation used in Spring Web applications. This annotation maps HTTP requests to handler methods of MVC and REST controllers. @RequestMapping(value = "/ex/foos", method = RequestMethod.GET) @ResponseBody public String getFoosBySimplePath() { return "Get some Foos"; } -@Path Variable @PathVariableannotation used to get variable name and its value at controller end. e.g. @RequestMapping(value = "/ex/foos/{fooid}/bar/{barid}", method = GET) @ResponseBody public String getFoosBySimplePathWithPathVariables (@PathVariable long fooid, @PathVariable long barid) { return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid; } -@RequestParam The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method. The @RequestParam annotation can be used with or without a value. The value specifies the request param that needs to be mapped to the handler method parameter, as shown in this code snippet. The default value of the @RequestParam is used to provide a default value when the request param is not provided or is empty. @RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/fetch", params = {"personId=10"})
  • 6.
    6ចងក្រងដោយ: ថនសារិ String getParams(@RequestParam("personId")String id){ return "Fetched parameter using params attribute = "+id; } @RequestMapping(value = "/fetch", params = {"personId=20"}) String getParamsDifferent(@RequestParam("personId") String id){ return "Fetched parameter using params attribute = "+id; } } -@Model Attribute @ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute and then exposes it to a web view. @ModelAttribute("person") public Person getPerson(){ return new Person(); } -@Request Header @RequestHeader that can be used to map controller parameter to request header value @Controller public class HelloController { @RequestMapping(value = "/hello.htm") public String hello(@RequestHeader(value="User-Agent") String userAgent) //.. } } -@Request Body @Request Body is Annotation indicating a method parameter should be bound to the body of the HTTP request. @RequestMapping(path = "/something",method = RequestMethod.PUT) public void handle(@RequestBody String body, Writer writer) throws IOException { writer.write(body); } -@Response Body The @ResponseBody annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name). @RequestMapping(path = "/something",method = RequestMethod.PUT)
  • 7.
    7ចងក្រងដោយ: ថនសារិ public @ResponseBodyString helloWorld() { return "Hello World"; }