In the notes explained in the previous chapters:
Http://www.cnblogs.com/EasonJim/p/6892280.html
Http://www.cnblogs.com/EasonJim/p/6899747.html
Http://www.cnblogs.com/EasonJim/p/6901115.html
Note that some of the above omissions, such as: @Repository, @Service, @Controller, @Component, now do the following additions:
Spring has introduced several annotations to simplify the development of spring since version 2.0. More comprehensive annotation functionality was introduced in the 2.5 release. @Repository annotations belong to the first batch introduced, which identifies the class of the data Access layer (DAO layer) as a spring Bean. Just label the note on the DAO class. Also, in order for spring to be able to scan classes in the Classpath and identify @repository annotations, you need to enable the bean's automatic scanning function in the XML configuration file, which can be <context:component-scan/> Realize. As shown below:
// First use @Repository to declare the DAO class as a Bean Span style= "color: #0000ff;" >package Com.jsoft.dao; @Repository public class Userdaoimpl implements userdao{...}
<!-- Second, start the Spring automatic scanning feature in the XML configuration file - < > ... < base-package/> ... </ Beans >
In this way, we no longer need to explicitly use <bean/> for bean configuration in XML. Spring will automatically scan all class files under the Base-package specified package and its sub-packages when the container is initialized, and all classes marked with @repository will be registered as spring beans.
Why @repository can only be labeled on the DAO class? This is because the annotation does not only recognize the class as a bean, but it also encapsulates the data access exception that is thrown in the annotated class as a spring data access exception type. Spring itself provides a rich and data-access exception structure that is independent of the specific data-access technology used to encapsulate exceptions thrown by different persistence-layer frameworks, making exceptions independent of the underlying framework.
Spring 2.5 Adds an additional three annotations with similar features on @repository: @Component, @Service, @Constroller, which are used for different levels of software systems:
- @Component is a generalization concept that simply represents a component (Bean) that can function at any level.
- @Service usually work in the business layer, but currently the function is the same as @component.
- @Constroller usually works on the control layer, but currently the function is the same as @component.
By using @repository, @Component, @Service, and @constroller annotations on classes, Spring automatically creates the corresponding Beandefinition objects and registers them in ApplicationContext. These classes become spring-managed components. These three annotations, in addition to classes that work at different levels of software, are used in exactly the same way as @repository.
In addition, in addition to the above four annotations, the user can create a custom annotation and then label @component on the annotation, then the custom annotation has the same functionality as the @component. However, this feature is not commonly used.
When a bean is automatically detected, its bean name is generated based on the Beannamegenerator policy of that scanner. By default, name is used as the name of the bean for @component, @Repository, @Service, and @controller that contain the Name property. If this annotation does not contain a name value or any other component found by the custom filter, the default bean name will be the unqualified class name at the beginning of the lowercase. If you do not want to use the default bean naming policy, you can provide a custom naming policy. First implement the Beannamegenerator interface, confirming that a default parameterless construction method is included. Then provide a fully qualified class name when configuring the scanner, as follows:
< ... > < base-package= "a.b" name-generator= "A.simplenamegenerator"/ ></beans>
Like the spring bean configured through XML, the bean identified by the above annotations has a default scope of "Singleton" (Singleton), in order to match these four annotations, the scope of the bean can be specified while labeling the Bean, Spring2.5 introduces the @ Scope annotations. You only need to provide the scope name when using the annotation, as follows:
@Scope ("prototype"publicclass
If you want to provide a custom scope resolution policy without using an annotation-based approach, simply implement the Scopemetadataresolver interface, confirming that a default constructor with no parameters is included. Then provide the fully qualified class name when configuring the scanner:
<base-package= "a.b" scope-resolver= "A.simplescoperesolver" />
Simple example:
DAO layer
Package Com.jsoft.dao; Public Interface Userdao { publicvoid Save ();}
DAO layer Implementation Class
Package Com.jsoft.dao.impl; Import org.springframework.stereotype.Repository; Import Com.jsoft.dao.UserDao; @Repository Public class Implements Userdao { @Override publicvoid Save () { System.out.println ( "Test Save");} }
Service Layer
Package Com.jsoft.service; Public Interface UserService { publicvoid usersave ();}
Service Implementation Class
PackageCom.jsoft.service.Impl;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportCom.jsoft.dao.UserDao;ImportCom.jsoft.service.UserService; @Service Public classUserserviceimplImplementsuserservice{@Autowired//Automatic injectionUserdao Userdao; @Override Public voidUsersave () {System.out.println ("Service Execution Save Method"); Userdao.save (); } }
DTO Intermediate Class
Package com.jsoft.dto; Import org.springframework.stereotype.Component; @Component Public class Dtoclass { publicvoid say () { System.out.println ("I am a dto");} }
Control layer
PackageCom.jsoft.Controller;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;ImportCom.jsoft.dto.DtoClass;ImportCom.jsoft.service.UserService; @Controller Public classUsercontroller {@AutowiredPrivateUserService UserService; @Autowired PublicDtoclass Dtoclass; Public voidSave () {System.out.println ("Control layer Save"); Dtoclass.say (); Userservice.usersave (); }}
Test class
Packagecom.jsoft.test.autoTest;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportCom.jsoft.Controller.UserController;ImportCom.jsoft.util.SpringUtil; Public classMyTest {applicationcontext ApplicationContext=NULL; @Before Public voidbefore () {ApplicationContext=Springutil.getapplicationcontext (); } @Test Public voidController () {Usercontroller Controller= Applicationcontext.getbean (Usercontroller.class); Controller.save (); }}
Reference:
http://blog.csdn.net/ye1992/article/details/19971467
Http://www.cnblogs.com/lonecloud/p/5745885.html
Spring annotations @repository, @Service, @Controller, @Component