@Repository, @Service, @Controller and @Component

Source: Internet
Author: User

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. Use annotations to register beans with the spring container. You need to register <context:component-scan base-package= "Pagkage1[,pagkage2,..., PagkageN]"/> in Applicationcontext.xml.

For example, specify a package in Base-package that automatically scans all annotations below the package

1 <context:component-scan base-package= "Cn.gacl.java"/>

Indicates the Cn.gacl.java package and its child packages, if a class has a specific annotation "@Component/@Repository/@Service/@Controller" on its head, This object is registered as a bean into the spring container. You can also specify multiple packages in the <context:component-scan base-package= ""/>, such as:

1 <context:component-scan base-package= "Cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action"/>

Multiple packages are separated by commas.

1. @Component

@Component
is a common form of all Spring management components,@Component annotations can be placed on the head of a class,@Component deprecated.

2. @Controller

@Controller The bean that corresponds to the presentation layer, which is the action, for example:

1 @Controller2 @Scope ("prototype") 3 public class Useraction extends baseaction<user>{4 ... 5}

after using the @controller annotation to identify the useraction, it means that the useraction is given to the spring container, and a "useraction" action exists in the spring container. This name is based on the Useraction class name. Note: If @Controller does not specify its value "@Controller", the default bean name is the first lowercase of the class name for this class, and if value is specified @Controller ( Value= "Useraction") " or " @Controller ("Useraction"), use value as the name of the Bean .

The useraction also uses the @scope annotation, @Scope ("prototype") that declares the scope of the action as a prototype and can take advantage of the container's scope= "prototype" To ensure that each request has a separate action to handle, avoiding the thread safety of the action in struts. Spring default scope is a singleton mode (scope= "singleton"), so that only one action object is created, each access is the same action object, the data is unsafe, and the STRUTS2 requires a different action for each access. Scope= "Prototype" ensures that an action object is created when a request is made

3. @ Service

The @Service corresponds to a business layer bean, for example:

1 @Service ("UserService") 2 public class Userserviceimpl implements UserService {3 ... 6?

The @Service ("UserService") annotation is to tell spring that when spring creates an instance of Userserviceimpl, the bean's name must be called "UserService", This way, when an action needs to use an instance of Userserviceimpl, it is possible to create a "userservice" from spring and inject it into the action: only one name called "UserService" is declared on the action. Variable to receive the "UserService" injected by spring, the code is as follows:

1//injected UserService2 @Resource (name = "UserService") 3 private UserService userservice;

Note: The type of the "userservice" variable in the action declaration must be "Userserviceimpl" or its parent class "UserService", otherwise it cannot be injected due to a type inconsistency because the declaration in the action " UserService "Variable uses the @resource annotation to label and indicates its name =" UserService ", which is tantamount to telling spring that my action is to instantiate a" UserService ", You spring quickly help me instantiate, and then give me, when spring sees the @resource annotation on the userservice variable, according to its specified name property, it is known that an instance of Userserviceimpl is needed in the action. At this point, spring will inject its own created Userserviceimpl instance called "UserService" into the "userservice" variable in the action, helping the action complete the instantiation of the UserService. This will not be passed in the action "userservice userservice = new Userserviceimpl ();" This is the most primitive way to instantiate UserService. If there is no spring, then when the action needs to use Userserviceimpl, it must pass "userservice UserService = new Userserviceimpl ();" The initiative to create an instance object, but after using spring, the action to use Userserviceimpl, you do not have to take the initiative to create an instance of Userserviceimpl, the creation of Userserviceimpl instance has been given to spring to do, Spring has created a good Userserviceimpl instance for action,action to get it straight. The action is used immediately after the original active creation of the Userserviceimpl instance and becomes passive waiting for the Userserviceimpl instance to be created by spring before being injected to action,action. This shows that the action on the "Userserviceimpl" class "control" has been "reversed", the original initiative in their own hands, to use the "Userserviceimpl" class instances, their own initiative to new one out immediately can be used, But now I can't take the initiative to go to the new "Userserviceimpl" class instance, newThe power of an instance of the "Userserviceimpl" class has been taken away by spring, and only spring is able to create an instance of the "Userserviceimpl" class, and the action can wait until spring creates "Userserviceimpl" Class, then "begs" spring to give him an instance of the created "Userserviceimpl" class so he can use "Userserviceimpl", which is spring's core thought "Control Reversal", also called"Dependency Injection"Dependency Injection" is also well understood, the action needs to use USERSERVICEIMPL work, then is the dependence on Userserviceimpl, spring to acion need to rely on the Userserviceimpl injection (that is, "give") To the action, this is called "Dependency injection". For action, the action relies on something, asks spring to inject it to him, and for spring, Spring takes the initiative to inject it into the action.

4, @ Repository

@Repository the corresponding data access layer bean, for example:

1 @Repository (value= "Userdao") 2 public class Userdaoimpl extends Basedaoimpl<user> {3 ... 4}

The @Repository (value= "Userdao") note is to tell spring to let spring create a Userdaoimpl instance named "Userdao".

When the service needs to use the Userdaoimpl instance named "Userdao" created by spring, you can use the @resource (name = "Userdao") annotation to tell Spring that Spring injects the created Userdao to the service.

1//injection Userdao, the user ID from the database to remove the specified user needs to use 2 @Resource (name = "Userdao") 3 private basedao<user> Userdao;

@Repository, @Service, @Controller and @Component

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.