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 the current 3comments and @Component are nothing new, but Spring will add special features to them in a future release. 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 code1. <?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 (including all child packages) @Service used for labeling business layer components, @Controller for labeling control layer components such as action in struts, @Repository for labeling data access Components, DAO components, @Component refer to components, which can be annotated when components are poorly categorized. */6. @Service Public classVentorserviceimplImplementsIventorservice {7.} @Repository Public classVentordaoimplImplementsIventordao {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 voiddestory () {11. } Injection method: Inject the DAO implementation class into the service implementation class, inject the service interface (note not the service implementation Class) into the action, not inject the new class, because spring will be injected automatically, If the error occurs manually again, then the property plus @autowired does not require getter () and setter () methods, and spring is automatically injected. As for more specific content, we will make a complete example of how to inject more skillfully. Note: In the spring configuration file, just add<context:annotation-config/> and <context:component-scan base- Package= "Package required to implement the injected class"/>, you can use base- Package="*"represents the entire class. <context:component-scan base- Package= "Com.eric.spring" >where base-package for the packet to be scanned (including all the child packages) before the interface labeled @autowired and @qualifier annotations so that the interface can be injected by the container, when the interface exists two implementation classes must specify one to inject, using the implementation of the first letter lowercase string to inject, such as: @Autowired @Qualifier ("Chinese") PrivateMans Man; 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 the bean name yourself."). @Controller is used to label a control-layer component, such as an action in struts, @Repository the persistence layer component, which is used to label the data access component, the DAO component @component a generic component, and when the component is poorly categorized, We can use this annotation for labeling. @Service Public classVentorserviceimplImplementsIventorservice {} @Repository Public classVentordaoimplImplementsthe default name of the Iventordao {} Getbean is the class name (lowercase letters), and if you want to customize it, you can specify it @service ("AAAAA"), which is a singleton by default, and if you want to change it, you can use @service (" Beanname ") @Scope (" prototype ") to change. You can specify the initialization method and the Destruction method (method name arbitrary) by using the following methods: @PostConstruct Public voidinit () {} @PreDestroy Public voiddestory () {}
Spring annotations @component, @Service, @Controller, @Repository