Spring's commonly used label @repository, @controller, @service

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

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


For example:

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

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

}

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

@Service is a Iserviceimpl class file for the service layer, and functions similar to @repository.



Another label: @autowired 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 the setter method.

We also have to have a configuration file 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>

This allows all of the. *daoimpl,.*serviceimpl under the Com.zxr.manager package to be registered 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.