Spring's Annotated form: @Repository, @Service, @Controller, which correspond to storage-layer beans, business-layer beans, and presentation- layer beans, respectively.
function@Repository, @Service, @Controller, and @Component
identify a class as a bean
Spring has introduced several annotations to simplify the development of spring since version 2.0 .
@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 achieved by <context:component-scan/>. As shown below:
How to usefirst, use the @Repository to declare the DAO class as a Bean
Package Bookstore.dao; @Repository
Second, start the Spring automatic scanning feature in the XML configuration file
<beans ... >
effect
In this way, we no longer need to explicitly use <bean/> for bean configuration in XML.
Spring automatically scans all class files under the specified Base-package package and its sub-packages when the container is initialized, and all classes labeled @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,
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 three additional annotations with similar features on @Repository basis:
@Component, @Service, @Constroller, respectively, 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 the class, spring automatically creates the corresponding Beandefinition object and registers it 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 annotate the annotation @Component, 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:
<beans ...> <context:component-scan base-package= "a.b" name-generator= "A.simplenamegenerator"/ >
Like the Spring bean configured through XML, the bean identified by the above annotations has a default scope of "singleton", in order to match these four annotations, the scope of the bean can be specified while labeling the bean, and the Spring2.5 introduces @Scope annotations. You only need to provide the scope name when using the annotation, as follows:
@Scope ("prototype") @Repository
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:
@Autowired
Finally, talk about @Autowired
Spring 2.5 introduces @Autowired annotations that allow you to annotate class member variables, methods, and constructors, and complete the task of automating Assembly. Use the @Autowired to eliminate the set, get method.
Package Com.baobaotao; public class Boss { //omit Get/setter @Autowired private car car; @Autowired Private Office office; @Override public String toString () { return "car:" + car + "/n" + "office:" + Office; } }
Reference: http://www.cnblogs.com/zhangzhifeng/p/4228498.html
Spring annotations: @Repository, @Service, @Controller, @Autowired