@Component, @Repository, @Service, @Controller, @Scope ("prototype") distinction

Source: Internet
Author: User

@Repository, @Service, @Controller, and @Component identify a class as a bean

Spring has been introduced since version 2.0 to simplify the development of spring. @Repository annotations are among the first to be introduced to identify the classes of the data Access layer (DAO layer) as Spring beans. Simply label the annotation on the DAO class. Also, in order for Spring to be able to scan classes in Classpath and identify @Repository annotations, you need to enable the automatic scanning of beans in the XML configuration file, which can be achieved by <context:component-scan/>. As shown below:

First use @Repository to declare the DAO class as Bean 
 package Bookstore.dao; 
 @Repository public 
 class Userdaoimpl implements userdao{...} 

 Second, start Spring's Auto Scan feature in an XML configuration file
 <beans ... > ...
 <context:component-scan base-package= "Bookstore.dao"/> ...
 </beans> 

In this way, we no longer need to explicitly use <bean/> for bean configuration in XML. Spring automatically scans for all class files under the specified package and its base-package when the container is initialized, and all classes that are annotated @Repository are registered as spring beans.

Why @Repository can only be labeled on DAO classes. This is because the annotation does not simply recognize the class as a bean, but it also encapsulates the data access exception that is thrown in the annotated class as the data access exception type for Spring. Spring itself provides a rich and independent data access exception structure that encapsulates the exceptions thrown by different persistence layer frameworks, leaving the exception to the underlying framework.

Spring 2.5 Adds additional three annotations with similar functionality on a @Repository basis: @Component, @Service, @Constroller, which are used at different levels of the software system: @Component is a generalization concept, Represents only one component (Bean) that can function at any level. @Service usually works in the business layer, but this feature is the same as @Component. @Constroller usually works on the control layer, but this function is the same as @Component.

By using @Repository, @Component, @Service, and @Constroller annotations on a class, spring automatically creates the appropriate Beandefinition object and registers it with the ApplicationContext. These classes become spring-managed components. These three annotations are used in exactly the same way as @Repository, except for classes that function at different levels of software.

In addition, in addition to the four annotations above, a user can create a custom annotation and then annotate @Component on the annotation, so that the custom annotation has the same functionality as the @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 that scanner. By default, name is taken as the Bean's name for @Component, @Repository, @Service, and @controller that contain the Name property. If this annotation does not contain a name value or a component that is found by a custom filter, the default Bean name is the unqualified class name that begins with lowercase. If you do not want to use the default bean naming policy, you can provide a custom named policy. The Beannamegenerator interface is implemented first, confirming that a default parameterless construction method is included. It then provides a fully qualified class name when the scanner is configured, as follows:

<beans ...> 
 <context:component-scan base-package= "a.b" name-generator= "A.simplenamegenerator" 
    > 
 </beans> 

Like a Spring bean configured through XML, the default scope of the bean, identified by the note above, is "singleton", in order to match the four annotations, the bean's scope can be specified while the bean is being annotated, Spring2.5 introduces @Scope annotations. You can use this annotation by providing only the name of the scope, as follows:

@Scope ("prototype") 
 @Repository public 
 class Demo {...} 

If you want to provide a custom scope resolution policy without using a method based on annotations, simply implement the Scopemetadataresolver interface and confirm that there is a default constructor method with no parameters. The fully qualified class name is then provided when the scanner is configured:

<context:component-scan base-package= "a.b"
 scope-resolver= "Footmark". Simplescoperesolver "/>
@Scope ("prototype") is really important
When we write a number of methods in an action class (in fact, a function of modular programming ideas), the return status of each method may be different, if the action is not @scope ("prototype"), it is possible to report the failure to find xxxaction. Writing this means that each request is recreated with an action, which corresponds to the Singalon, commonly known as "multiple cases."
 By default, the instance obtained from the Bean Factory is Singleton (The Bean's Singleton property) singleton:spring The container only has one shared bean instance,  the default configuration. Prototype: A new bean instance is created each time a request is made to the bean. The chosen principle is that stateful beans use the prototype scope, whereas stateless beans should use the singleton scope.   in Spring2.0 has added three new web scopes in addition to previous Singleton and prototype, request, session, and global session respectively. such as   you want a bean in a container to have one of the new web scopes, in addition to configuring the appropriate scope property at the bean level, you must make an additional initial   configuration at the container level. That is, adding such a contextlistener:  to the web.xml of Web applications Org.springframework.web.context.request.RequestContextListener This is a later version of Servlet 2.4. such as the request scope.
Reference to:http://wdlisoft.javaeye.com/blog/479049  Singleton is a single state mode, with the IOC container management, of course, is not thread-safe, but the so-called thread safety is also relative   If your class is not stateful, the performance of the singleton is higher because there is only one instance.   If your class is stateful, then you must display the setting to prototype   in the SSH2 project, when Struts2 's action is left to spring management, spring defaults to Singleton,
The Struts2 action is clearly stateful, so you must display the set to  scope= "prototype"   prototype as prototype mode, and each action request will create an action   But the implementation class of those DAO is promoted scope= "singleton", because these classes have no state, with Singleton only need to maintain an instance, obviously the performance is high some 

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.