Spring commonly used tags @repository, @controller, @service

Source: Internet
Author: User
Tags aop xmlns
In the Annotaion configuration note, @component is used to represent a generic comment to illustrate that a class is a spring container-managed class. That is, the class has been pulled into the management of spring. And @controller, @Service, @Repository is the refinement of @component, these three annotations with more semantics than @component, they correspond to the control layer, service layer, the class of the persistence layer respectively.

The @Repository tag is used to define a name for the class of the persistence layer, so that spring is associated with the class based on the name.


For example:

@Repository ("Userdao")
public class Userdaoimpl implements userdao{

........................................

}

Declares the name of Userdaoimpl in the spring container called Userdao.

The @Service is a Iserviceimpl class file for the service layer, similar in function to @repository.



Another tag: @autowired is used to inject.

For example:

@autowired
Private Userdao Userdao;

This is injected into the equivalent of a new implementation class, so we don't have to write setter methods.

We also have to have configuration files to configure:

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<context:annotation-config/>
<context:component-scan base-package= "Com.zxr.manager" >
<context:include-filter type= "regex" expression= ". *daoimpl"/>
<context:include-filter type= "regex" expression= ". *serviceimpl"/>
</context:component-scan>
</beans>

In this way, all the. *daoimpl,.*serviceimpl under the Com.zxr.manager package are registered to be associated with the spring container.

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.