For Spring annotation @ Service @ Component @ Controller @ Repository usage, @ service @ component

Source: Internet
Author: User

For Spring annotation @ Service @ Component @ Controller @ Repository usage, @ service @ component

@ Component is equivalent to the object of the instantiation class. The other three annotations can be understood as the subannotation or refinement of @ Component.

In the annotaion configuration annotation, @ Component is used to indicate that a class is a spring container management class, which includes spring scans and containers for ioc. That is, the class has been pulled into spring management.

In classpath, components are automatically scanned and managed by spring containers.

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. A http://www.springframework.org/schema/beans/spring-beans-2.5. xsd
  7. Http://www.springframework.org/schema/context
  8. The 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 the java files under the base-pack or sub-packages. If the classes with the @ Component @ Controller @ Service annotations are scanned, register these classes as bean -->
  12. </Beans>

 

 

Note: To use annotations, you must configure <context: annotation-config/>. However, if @ Component is used, you do not need to add it because: <context: component-scan base-package = "com. zchen "> the default value is <context: annotation-config/>.

@ Controller, @ Service, @ Repository is the refinement of @ Component. These three annotations have more semantics than @ Component, they correspond to the control layer, service layer, and persistent layer classes respectively.

@ Component refers to a Component. When the Component is difficult to classify, we can use this annotation for annotation (This annotation can be used now, but only a single Component can be used)

Java code

@ Repository label is used to define a name for the class of the persistent layer and associate Spring with this class according to this name.

For example:

@ Repository (value = "userDao") ----- You can also directly write @ Repository ("userDao"). If there is no name, the default name is class name: UserDaoImpl.
Public class UserDaoImpl implements UserDao {
........................................
}

Make sure that this class is in bean. xml under <context: component-scan base-pakage = "com. czhen"> com. czhen pakage dir.

After Repository is annotated, @ autowire can be directly injected into managerImpl, for example:

 

@ Autowire

Private UserDaoImpl userDaoImpl;

 

In general, @ Controller (control layer) is the action entry. When @ Service (Service layer) is called, the Service calls @ Repository (persistence layer) to complete data persistence.

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.