Spring's Annotated form: @Repository, @Service, @Controller,

Source: Internet
Author: User

Spring's Annotated form: @Repository, @Service, @Controller, which correspond to storage-layer beans, business-layer beans, and presentation-layer beans, respectively.


@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:

The DAO class is first declared as a Bean package Bookstore.dao using @Repository  ;  @Repository public  class Userdaoimpl implements userdao{...}  Second, start Spring's automatic scanning function in the XML configuration file <beans ... >     

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, 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 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:

Spring's Annotated form: @Repository, @Service, @Controller,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.