Spring 3 annotations
context:property-placeholder:
====================
This configuration is used to configure property placeholders
and one or more locations can be provided with a comma-separated
list in the location attribute.
Example:
<context:property-placeholder location="classpath:com/xyz/jdbc.properties"/>
<context-annotation-config/>
===================
This configuration looks for annotations on beans in the application context & only inspects for Auto-wired beans in your controllers.
context:component-scan
=================
This configuration scans beans for @Controller, @Service, @Repository etc in base-package annotated classes.
Example single base package:
<context:component-scan base-package="com.xyz"/>
Example multiple base packages:
<context:component-scan base-package="com.xyz,com.abc,.. ."/>
base-package is comman parent package for @Controller, @Service, @Repository etc.
context:include-filter & context:exclude-filter:
These are chaild tags for “context:component-scan” you can include & discard classes scan beans.
Example:
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression=".*xyx.*Service"/>
<context:exclude-filter type="annotation" expression="com.xyz.Repository"/>
</context:component-scan>
@Controller:
============
Class which is annotated with it acts as spring controller
@RequestMapping
=================
This annotation maps web requests onto specific controller classes
and/or methods of it.
@Controller
@RequestMapping("hello") – maps to controller class
public class HelloController
{
@RequestMapping(value ="/welcome", method =
RequestMethod.GET) – maps to specific method of controller class
public String welcome(Model model) {
--------
}
}
@RequestParam
=============
This annotation is used to bind request parameters to a method parameter
in controllers.
Example:
========
public String addEmp(@RequestParam("empId") int empId,
@RequestParam("empName") String empName, ModelMap model) {-----}
@Scope:
=======
This annotation is used to specify scope of the beans.
Example:
@Scope("request"), @Scope("prototype")
@Service
========
Class which is annotated with it acts as service class(Contains bussiness logic).
@Repository:
============
Class which is annotated with it acts as DAO class(Contains Data Access Object
-JDBC logic).
@Component:
===========
This annotation is used generic spring mannaged component.
@Resource:
==========
This annotation is used on bean fileds/setter-methods and it have name attribute.
Spring interprets the name and injects specified bean.
@Qualifier
=========
This annotation gives more control over the selection process of bean
and allows for associating qualifier values with specific arguments.
It removes ambiguity in bean wiring with auto-wire.
It narrows the set of type matches so that assigns specific bean.
Example:
@Autowired
@Qualifier("searchService ")
private SearchServiceImpl searchService;
@Required:
==========
This annotation is used to appliy on bean properties/setter-methods.
Particular property/setter-methods of class must be provided
otherwise the configuration fails.
Example:
@Required
public void setEmployee(Employee emp)
{
this. emp = emp;
}
@PathVariable:
==============
This annotation is used on a spring controller method argument to bind it
to the value of a URI template variable.
Example:
@RequestMapping(value="/employees/{empId}", method=RequestMethod.GET)
public String findEmp(@PathVariable String empId, Model model) {
Employee emp = empService.findEmo(empId);
---------
}
@Value
======
This annotation is used to inject value of member variable from property file.
Example
private @Value("#{jdbcProperties.url}") String jdbcUrl;
private @Value("#{jdbcProperties.username}") String username;
private @Value("#{jdbcProperties.password}") String password;
@PostConstruct and @PreDestroy
=================================
These annotations offers initialization callbacks and destruction callbacks.
Example:
Class MyDataSource {
Connection con = null;
@PostConstruct
public Connection getConnction() {
----------
}
@PreDestroy
public void clearConnection () {
-----------
}
}
@ModelAttribute:
================
This annotation binds a method parameter or method return value to a named model attribute.
Example:
Binding a method parameter:
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("employee") Employee employee,
BindingResult result, SessionStatus status) {
--------
}
Binding a method return value:
@ModelAttribute("booksList")
public List<String> populateBooksListList() {
List<String> booksList = new ArrayList<String>();
booksList.add("Spring MVC");
booksList.add("Oracle");
booksList.add("Java");
booksList.add("JSF");
return booksList;
}
context:property-placeholder:
====================
This configuration is used to configure property placeholders
and one or more locations can be provided with a comma-separated
list in the location attribute.
Example:
<context:property-placeholder location="classpath:com/xyz/jdbc.properties"/>
<context-annotation-config/>
===================
This configuration looks for annotations on beans in the application context & only inspects for Auto-wired beans in your controllers.
context:component-scan
=================
This configuration scans beans for @Controller, @Service, @Repository etc in base-package annotated classes.
Example single base package:
<context:component-scan base-package="com.xyz"/>
Example multiple base packages:
<context:component-scan base-package="com.xyz,com.abc,.. ."/>
base-package is comman parent package for @Controller, @Service, @Repository etc.
context:include-filter & context:exclude-filter:
These are chaild tags for “context:component-scan” you can include & discard classes scan beans.
Example:
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression=".*xyx.*Service"/>
<context:exclude-filter type="annotation" expression="com.xyz.Repository"/>
</context:component-scan>
@Controller:
============
Class which is annotated with it acts as spring controller
@RequestMapping
=================
This annotation maps web requests onto specific controller classes
and/or methods of it.
@Controller
@RequestMapping("hello") – maps to controller class
public class HelloController
{
@RequestMapping(value ="/welcome", method =
RequestMethod.GET) – maps to specific method of controller class
public String welcome(Model model) {
--------
}
}
@RequestParam
=============
This annotation is used to bind request parameters to a method parameter
in controllers.
Example:
========
public String addEmp(@RequestParam("empId") int empId,
@RequestParam("empName") String empName, ModelMap model) {-----}
@Scope:
=======
This annotation is used to specify scope of the beans.
Example:
@Scope("request"), @Scope("prototype")
@Service
========
Class which is annotated with it acts as service class(Contains bussiness logic).
@Repository:
============
Class which is annotated with it acts as DAO class(Contains Data Access Object
-JDBC logic).
@Component:
===========
This annotation is used generic spring mannaged component.
@Resource:
==========
This annotation is used on bean fileds/setter-methods and it have name attribute.
Spring interprets the name and injects specified bean.
@Qualifier
=========
This annotation gives more control over the selection process of bean
and allows for associating qualifier values with specific arguments.
It removes ambiguity in bean wiring with auto-wire.
It narrows the set of type matches so that assigns specific bean.
Example:
@Autowired
@Qualifier("searchService ")
private SearchServiceImpl searchService;
@Required:
==========
This annotation is used to appliy on bean properties/setter-methods.
Particular property/setter-methods of class must be provided
otherwise the configuration fails.
Example:
@Required
public void setEmployee(Employee emp)
{
this. emp = emp;
}
@PathVariable:
==============
This annotation is used on a spring controller method argument to bind it
to the value of a URI template variable.
Example:
@RequestMapping(value="/employees/{empId}", method=RequestMethod.GET)
public String findEmp(@PathVariable String empId, Model model) {
Employee emp = empService.findEmo(empId);
---------
}
@Value
======
This annotation is used to inject value of member variable from property file.
Example
private @Value("#{jdbcProperties.url}") String jdbcUrl;
private @Value("#{jdbcProperties.username}") String username;
private @Value("#{jdbcProperties.password}") String password;
@PostConstruct and @PreDestroy
=================================
These annotations offers initialization callbacks and destruction callbacks.
Example:
Class MyDataSource {
Connection con = null;
@PostConstruct
public Connection getConnction() {
----------
}
@PreDestroy
public void clearConnection () {
-----------
}
}
@ModelAttribute:
================
This annotation binds a method parameter or method return value to a named model attribute.
Example:
Binding a method parameter:
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("employee") Employee employee,
BindingResult result, SessionStatus status) {
--------
}
Binding a method return value:
@ModelAttribute("booksList")
public List<String> populateBooksListList() {
List<String> booksList = new ArrayList<String>();
booksList.add("Spring MVC");
booksList.add("Oracle");
booksList.add("Java");
booksList.add("JSF");
return booksList;
}