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
- <? 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: context = "http://www.springframework.org/schema/context"
- Xsi: schemaLocation = "http://www.springframework.org/schema/beans
- A http://www.springframework.org/schema/beans/spring-beans-2.5. xsd
- Http://www.springframework.org/schema/context
- The http://www.springframework.org/schema/context/spring-context-2.5. xsd ">
- <! -- <Context: annotation-config/> -->
- <Context: component-scan base-package = "com. zchen"/>
- <! -- 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 -->
- </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.