Spring annotations @component, @Repository, @Service, @Controller differences

Source: Internet
Author: User

For a long time did not do Web projects have been learned that the point of the framework of knowledge forgotten, today to do a previous project to look at the discovery of a component with a @Component, and online some with @service labeled components, I faint to check the information:

In addition to providing @Component annotations in spring 2.5, several annotations with special semantics are defined, namely, @Repository, @Service, and @Controller.
In the current Spring version, these 3 comments and @Component are equivalent, but from the name of the annotation class it is easy to see that the 3 annotations correspond to the persistence layer, the business layer, and the control layer (the WEB layer), respectively.
Although there is nothing new about these 3 annotations and @Component, Spring will add special features to them in future releases.
Therefore, if a WEB application uses a classic three-tier hierarchy, it is best to annotate the classes in the hierarchy with the above annotations, respectively, in the persistence layer, the business layer, and the control layer.

@Service for labeling business layer components

@Controller for labeling control-layer components, such as action in struts

@Repository for labeling data Access Components, DAO components

@Component refer to components, we can use this annotation when the component is poorly categorized.

1 @Service 2  Public class Implements Iventorservice {   3}4@Repository5public  classimplements6 }

In a slightly larger project, if the component is configured with an XML bean definition, it will obviously increase the volume of the configuration file, and it may not be easy to find and maintain.

SPRING2.5 introduced the component automatic scanning mechanism, where he looked for classes labeled the above annotations in the classpath and managed them into the spring container.
Its role is the same as when you configure a component with a bean node in an XML file. To use the automatic scanning mechanism, we need to open the following configuration information:

Code

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5Xsi:schemalocation= "Http://www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans-2.5.xsd7http//Www.springframework.org/schema/context8http//www.springframework.org/schema/context/spring-context-2.5.xsd ">9    Ten<context:component-scan base- Package= "Com.eric.spring" > One</beans>

The 1.component-scan tag automatically scans packages (with all child packages) under the specified path by default, automatically registering classes with @component, @Repository, @Service, @Controller tags to the spring container. To mark the Spring's @Required, @Autowired, JSR250 's @PostConstruct, @PreDestroy, @Resource, Jax-ws ' s @WebServiceRef, EJB3 ' s @ EJB, JPA's @PersistenceContext, @PersistenceUnit, and other annotated classes make annotations effective (including the role of the annotation-config tag).

The default name for Getbean is the class name (lowercase letter), which you can specify @service ("AAAAA") if you want to customize it.
This bean is "singleton" by default, and if you want to change it, you can use @scope ("prototype") to change it.

You can specify the initialization method and the destruction method in the following ways:

1 @PostConstruct 2  Public void  3 4 @PreDestroy 5 public void   6

Injection method:

Inject the DAO implementation class into the service interface of the action (note not the implementation class of the service), and do not inject the class as it is injected, because spring will be injected automatically, and if it is manually re-new, there will be an error.
The getter () and setter () methods are not required after the attributes are added to the @autowired, and spring is automatically injected.

@autowired annotations on the front of the interface allow the interface to be injected into the container, such as:

1 @Autowired 2 @Qualifier ("Chinese")3Private

When there are two implementation classes for an interface, you must specify which implementation class to inject with @qualifier, otherwise you can omit it and write @autowired only.

Reprinted from: http://blog.csdn.net/zhang854429783/article/details/6785574

Spring annotations @component, @Repository, @Service, @Controller differences

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.