Spring Annotations Component Repository Service Controller differences

Source: Internet
Author: User


In addition to providing @Component annotations in Spring 4.1, 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 by using @Repository, @Service, and @Controller, respectively, in the persistence layer, the business layer, and the control layer @Component Comment on those classes that are more neutral.


Java code

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "http://www.springframework.org/schema/ Context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xsi:schemalocation="/HTTP/ www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp ://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd ">< Context:annotation-config/><context:component-scan base-package= "com"/></beans></span>


Where Base-package is a package that needs to be scanned (including all child packages) @Service used for labeling business layer components, @Controller for labeling control layer components such as action in struts, @Repository for labeling data access Components, The DAO component, and @component refers to the component, we can use this annotation to annotate when the component is poorly categorized.


@Service public class Ventorserviceimpl implements Iventorservice {
} @Repository public class Ventordaoimpl implements Iventordao {
The default name for Getbean is the class name (lowercase letters), and if you want to customize it, you can specify it @service ("AAAAA"), which defaults to Singleton, and if you want to change it, you can use @service ("Beanname") @Scope (" Prototype ") to change. You can specify the initialization method and the Destruction method (method name arbitrary) by using the following method: @PostConstruct public void init () {
}
@PreDestroy public void Destory () {
}


Injection method:

Inject the DAO implementation class into the service implementation class and inject the service interface (note not the service implementation Class) into the action, note

Do not new this injected class, because spring will be automatically injected, if the manual re-new words will be an error, and then the property plus

The getter () and setter () methods are not required after @Autowired, and spring is automatically injected. As for more specific content, the way to inject is more

When you are proficient, you will be given a complete example.

Annotations:

In the spring configuration file only need to add <context:annotation-config/> and <context:component-scan base-package= "need to implement the injected class package"/ You can use base-package= "*" to represent all classes.

<context:component-scan base-package= "Com.eric.spring" >

Where Base-package is the package that needs to be scanned (with all child packages)

The interface is labeled with @autowired and @qualifier annotations so that the interface can be injected by the container, when the interface exists two implementation classes must be specified one to inject, using the implementation of the first letter lowercase string to inject, such as:

    1.     @Autowired      
    2.   
    3.     @ Qualifier ("Chinese")       
    4.   
    5.     private man man;   

Otherwise you can omit, write only @autowired.

@Service service layer component, which is used to label business layer components, to define a bean, automatically instantiate a lowercase bean based on the Bean's class name, for example Chinese to Chinese, and if you need to change the name yourself: @Service (" You changed your own bean name ").

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

@Repository persistence layer component, which is used to label data access Components, DAO components

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


@Service
public class Ventorserviceimpl implements Iventorservice {
}

@Repository
public class Ventordaoimpl implements Iventordao {
}

The default name of the Getbean is the class name (lowercase letters), and if you want to customize it, you can specify it @service ("AAAAA"), which

The bean default is singleton, and if you want to change it, you can use the @service ("Beanname") @Scope ("prototype") to change it.

You can specify the initialization method and the Destruction method (method name arbitrary) by using the following methods:

@PostConstruct

public void init () {

}

@PreDestroy

public void Destory () {
}



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.