@Component @Controller @Repository Usage of spring annotations @Service

Source: Internet
Author: User

@Component equivalent to an object that instantiates a class, the other three annotations can be interpreted as sub-annotations or refinements of @component.

In the Annotaion configuration note, @component is used to denote a generic comment to illustrate that a class is a spring container-managed class that will have spring scan and join the container to participate in the IOC. That is, the class has been pulled into the management of spring.

The build is incorporated into spring container management by means of automatic scanning in classpath.

To use the automatic scanning mechanism we need to open the configuration information:

Bean.xml Code

  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. Xsi:schemalocation= "Http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5. xsd
  7. Http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5. xsd ">
  9. <!--<context:annotation-config/>-->
  10. <context:component-scan base-package="Com.zchen"/>
  11. <!--contains annotation-config. Spring can automatically scan Java files under Base-pack or under the sub-package, and if scanned into classes with annotations such as @component @[email protected], register these classes as Beans-- >
  12. </beans>

Note: The preceding remarks need to be configured to use annotations: <context:annotation-config/> But if you use @component, you do not need to add it because: <context:component-scan Base-package= "Com.zchen" > <context:annotation-config/> is the default.

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.

@Component refer to the component, we can use this annotation when the component is not classified, (we can use this annotation now, we can work with only a single component)

Java code

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 (value= "Userdao")-----can also be directly written @repository ("Userdao"), if not named, the default name is class Name:userdaoimpl
public class Userdaoimpl implements userdao{
........................................
}

Ensure that this class is <context:component-scan base-pakage= "Com.czhen" >com.czhen pakage dir in Bean.xml.

Annotations can be directly injected into the Managerimpl using @autowire directly after repository, such as:

@Autowire

Private Userdaoimpl Userdaoimpl;

In general @controller (Control layer) is the action portal, called @service (Business layer), and service calls @repository (persistence layer) to complete data persistence.

@Component @Controller @Repository Usage of spring annotations @Service

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.