In addition to providing @Component annotations in Spring 2.5, http://www.blogjava.net/kangdy/archive/2011/10/10/360402.html also defines several annotations that have special semantics. They are: @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 by using @Repository, @Service, and @Controller, respectively, in the persistence layer, the business layer, and the control layer @Component Comment on those classes that are more neutral.
In a slightly larger project, there are usually hundreds of components, and if these components are configured with an XML bean definition, it will obviously increase the volume of the configuration file, and it may not be convenient to find and maintain them. Spring2.5 introduced the component Auto-scan mechanism, where he could look under the classpath to annotate @Component, @Service, @Controller, @Repository annotated classes, and incorporate these classes into the spring container for management. Its role is the same as when you configure a component with a bean node in an XML file. To use the automatic scanning mechanism, we need to open the following configuration information:
Java code
1. <?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd "
2. >
3.
4. <context:component-scan base-package= "Com.eric.spring" >
5. </beans>
/* Where Base-package is the package that needs to be scanned (with all child packages)
@Service for labeling business layer components,
@Controller is used to label control-layer components, such as action in struts,
@Repository is used to label data access Components, DAO components,
@Component refer to components, we can use this annotation when the component is poorly categorized.
*/
6. @Service public class Ventorserviceimpl implements Iventorservice {
7.} @Repository public class Ventordaoimpl implements Iventordao {
8.}
The default name of the/*getbean is the class name (lowercase letters), if you want to customize, you can specify @service ("AAAAA"), the bean default is a singleton, if you want to change, you can use the @Service ("Beanname") @Scope (" Prototype ") to change. You can specify the initialization method and the Destruction method (method name arbitrary) by using the following method: @PostConstruct public void init () {
*/
9.}
Ten. @PreDestroy public void Destory () {
11.}
Injection method:
Inject the DAO implementation class into the service implementation class, inject the service's interface (note not the service's implementation class) into the action, and inject it without the new injected class, because spring will be injected automatically, and if it is manually re-new, there will be an error. The getter () and setter () methods are not required after the attributes are added to the @autowired, and spring is automatically injected. As for more specific content, we will make a complete example of how to inject more skillfully.
Annotations:
In the spring configuration file only need to add <context:annotation-config/> and <context:component-scan base-package= "need to implement the injected class package"/ You can use base-package= "*" to represent all classes.
<context:component-scan base-package= "Com.eric.spring" >
Where Base-package is the package that needs to be scanned (with all child packages)
The interface is labeled with @autowired and @qualifier annotations so that the interface can be injected by the container, when the interface exists two implementation classes must be specified one to inject, using the implementation of the first letter lowercase string to inject, such as:
- @Autowired
- @Qualifier ("Chinese")
- Private Man Mans;
Otherwise you can omit, write only @autowired.
@Service service layer component, which is used to label business layer components, to define a bean, automatically instantiate a lowercase bean based on the Bean's class name, for example Chinese to Chinese, and if you need to change the name yourself: @Service (" You changed your own bean name ").
@Controller for labeling control-layer components, such as action in struts
@Repository persistence layer component, which is used to label data access Components, DAO components
@Component refer to components, we can use this annotation when the component is poorly categorized.
@Service
public class Ventorserviceimpl implements Iventorservice {
}
@Repository
public class Ventordaoimpl implements Iventordao {
}
The default name of the Getbean is the class name (lowercase letters), and if you want to customize it, you can specify it @service ("AAAAA"), which
The bean default is singleton, and if you want to change it, you can use the @service ("Beanname") @Scope ("prototype") to change it.
You can specify the initialization method and the Destruction method (method name arbitrary) by using the following methods:
@PostConstruct
public void init () {
}
@PreDestroy
public void Destory () {
}
Spring annotations @component, @Service, @Controller, @Repository