@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
- <?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
- http://www.springframework.org/schema/beans/spring-beans-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.zchen"/>
- <!--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-- >
- </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