Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
One, annotation-based configuration
In addition to providing @Component annotations in Spring 2.5, several annotations with special semantics are defined, namely, @Repository, @Service, and @Controller.
In the current Spring version, these 3 comments and @Component are equivalent, but from the name of the annotation class it is easy to see that the 3 annotations correspond to the persistence layer, the business layer, and the control layer (the WEB layer), respectively.
Although there is nothing new about these 3 annotations and @Component, Spring will add special features to them in future releases. Therefore, if a WEB application uses a classic three-tier hierarchy, it is best to annotate the classes in the hierarchy with the above annotations, respectively, in the persistence layer, the business layer, and the control layer.
1. @controller controller (injection service)
2. @service service (injected into DAO)
3. @repository dao (for DAO Access)
4, @component (the normal pojo is instantiated into the spring container, equivalent to the <bean id= "" class= ""/> "in the configuration file)
@component usage is as follows:
@Component ("Userdao") public class Userdaoimp implements userdao{@Overridepublic void Add (user user) { System.out.println (User.getname ()); System.out.println (User.getsex ()); System.out.println (User.getage ()); }}
Use the @component annotation on the class to indicate that the class is defined as a spring management bean, using the default value (optional) attribute to represent the bean identifier.
Equivalent:
<bean id= "Userdao" class= "Com.mucfc.dao.UserDao"/>
@Component, @Service, @Controller, @Repository annotated classes, and incorporate these classes into the spring container for management.
The following is a scan component that introduces component
<!--configured Bean package Location-- <context:component-scan base-package= "COM.MUCFC"/>
Where Base-package is the package that needs to be scanned (with all child packages)
@Component Refer to components, we can use this annotation when the component is poorly categorized.
Ii. Examples of Use
User.java: Annotated with @repository
Package Com.mucfc.model;import Org.springframework.context.annotation.scope;import org.springframework.stereotype.Repository; @Repository ("User") @Scope ("prototype")//@Repository used to label data access Components, DAO components @Scope ("prototype") to return a different instance each time, the default is a singleton public class User {private String name;private string Sex;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} public int getage () {return age;} public void Setage (int.) {this.age = age;} private int age;}
Userdao.java
Package Com.mucfc.dao;import Com.mucfc.model.user;public interface Userdao {public void Add (user user);
Userdaoimp.java: Annotated with @controller
Package Com.mucfc.daoimp;import Org.springframework.stereotype.controller;import Com.mucfc.dao.userdao;import Com.mucfc.model.User; @Controller ("Userdao")//@Controller for labeling control-level components public class Userdaoimp implements userdao{@ overridepublic void Add (user user) {System.out.println (User.getname ()); System.out.println (User.getsex ()); System.out.println (User.getage ()); }}
Userservice.java: Annotated with @service
Package Com.mucfc.service;import Javax.annotation.resource;import Org.springframework.stereotype.service;import Com.mucfc.dao.userdao;import Com.mucfc.model.User; @Service ("UserService")//@Service for labeling business layer components public class UserService {//setter injection interface, interface-oriented programming private Userdao Userdao; public void Add (user user) { userdao.add (user); } Public Userdao Getuserdao () { return userdao; } Note @Resource (name= "Userdao") public void Setuserdao (Userdao userdao) { This.userdao = Userdao; } }
Beans.xml '
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/ Schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- The configured Bean's package is located-- <context:component-scan base-package= "COM.MUCFC"/></beans>
Under test:
Package Com.mucfc.anno;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.mucfc.model.user;import Com.mucfc.service.userservice;public class Test {public static void main (string[] args) {ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Beans.xml"); UserService service= (UserService) Applicationcontext.getbean ("UserService"); User User1 = (user) Applicationcontext.getbean ("User"); User User2 = (user) Applicationcontext.getbean ("user"); User1.setname ("xiaoming"); User1.setsex ("male"); User1.setage (22); User2.setname ("Little Red"), User2.setsex ("female"), User2.setage (), Service.add (user1); Service.add (User2);}}
Results:
when a specific annotation is used in a component class, the <CONTEXT:COMPONENT-SCAN> is also declared in the spring configuration file :
- The Base-package property specifies a base class package that needs to be scanned, and the spring container will scan all classes in the entire base class package and its child packages
- When you need to scan multiple packages, you can use commas to separate
- If you want to scan only specific classes, not all classes under the base package, you can use the Resource-pattern property to filter specific classes, instances:
- <context:include-filter> child nodes represent the target classes to be included
- <context:exclude-filter> child nodes indicate the target class to exclude
- <context:component-scan> can have a number of <context:include-filter> and <context:exclude-filter> sub-nodes
- <context:include-filter> and <context:exclude-filter> sub-nodes support multiple types of filter tables
Three, more configuration meta-data
1. @Lazy: The definition bean will defer initialization, using the following method:
Java code:
@Component ("Component") @Lazy (true) public class Testcompoment {...}
Use @lazy annotations to specify that the bean requires lazy initialization.
2.@DependsOn: define the order of bean initialization and destruction, using the following methods:
Java code:
@Component ("Component") @DependsOn ({"Managedbean"}) public class Testcompoment {...}
3, @Scope: Define the bean scope, the default singleton, using the following methods:
Java code:
@Component ("Component") @Scope ("Singleton") public class Testcompoment {...}
4, @Qualifier: Specify the qualifier descriptor, corresponding to the <qualifier> tag in the XML configuration, using the following methods:
Java code:
@Component ("Component") @Qualifier ("Component") public class Testcompoment {...}
You can use complex extensions, such as @mysql, and so on.
5, @Primary: Automatic assembly When multiple bean candidates appear, the bean annotated as @primary will be the preferred one, otherwise an exception will be thrown, using the following method:
Java code:
@Component ("Component") @Primarypublic class Testcompoment {...}
Lin Bingwen Evankaka original works. Reprint please specify the source Http://blog.csdn.net/evankaka
Note-based configuration bean of the "Spring" IOC