@ Repository, @ service, @ controller, and @ component

Source: Internet
Author: User

@ Repository, @ service, @ controller, and @ component identify the class as bean

Since version 2.0, some annotations have been introduced to simplify spring development. @ Repository annotation is the first batch introduced. It is used to identify the classes of the data access layer (DAO layer) as spring bean. You only need to mark the Annotation on the DAO class. In addition, to enable spring to scan the classes in the class path and identify the @ repository annotation, you must enable the bean automatic scanning function in the XML configuration file. You can use the <context: component-Scan/>. As follows:


// Use @ repository to declare the DAO class as bean

Package bookstore. Dao;

@ Repository

Public class userdaoimpl implements userdao {...... }


// Second, enable spring's automatic scanning function in the XML configuration file

<Beans... >

......

<Context: component-scan base-package = "Bookstore. Dao"/>

......

</Beans>


In this way, you no longer need to explicitly use <bean/> in XML for bean configuration. Spring will automatically scan all class files in the package specified by the base-Package and its sub-packages during container initialization. All classes marked with @ repository will be registered as spring bean.


Why can only @ repository be labeled on the DAO class? This is because the annotation not only recognizes classes as beans, but also encapsulates the data access exceptions thrown in the labeled classes as spring Data Access exceptions. Spring itself provides a rich data access exception structure unrelated to specific data access technologies. It is used to encapsulate exceptions thrown by different persistence layer frameworks, this makes exceptions independent from the underlying framework.


Spring 2.5 adds three additional annotations with similar functions on the basis of @ Repository: @ component, @ service, and @ constroller, which are used for different layers of the software system:


@ Component is a general concept. It only represents a component (bean) and can act at any level.

@ Service is usually used at the business layer, but currently this function is the same as @ component.

@ Constroller is usually used in the control layer, but currently this function is the same as @ component.

By using the @ 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. In addition to the classes at different software levels, these three annotations are used in exactly the same way as @ repository.


In addition to the preceding four annotations, you can create custom annotations and add @ component to the annotations, this custom annotation has the same functions as @ component. However, this function is not commonly used.


When a bean is automatically detected, its bean name is generated based on the beannamegenerator policy of the scanner. By default, for @ component, @ repository, @ service, and @ controller that contain the name attribute, the name value is used as the bean name. If this annotation does not contain the name value or other components found by the custom filter, the default bean name will be a non-qualified class name starting with 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 and confirm that a default non-parameter constructor is included. Then, a fully qualified class name is provided when the scanner is configured, as shown below:


<Beans...>

<Context: component-Scan

Base-package = "A. B" name-generator = "A. simplenamegenerator"/>

</Beans>


Similar to spring beans configured through XML, the default scope of beans identified by the preceding annotation is "Singleton". To work with these four annotations, while marking beans, you can specify bean scopes. spring2.5 introduces the @ scope annotation. You only need to provide the scope name when using this annotation, as shown below:


@ Scope ("prototype ")

@ Repository

Public class demo {... }


If you want to provide a custom scope resolution policy without using annotation-based methods, you only need to implement the scopemetadataresolver interface and confirm that it contains a default constructor without parameters. Then, a fully qualified class name is provided when the scanner is configured:


<Context: component-scan base-package = "A. B"

Scope-resolver = "footmark. simplescoperesolver"/>


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.