The difference between spring annotation @Component, @Repository, @Service, @Controller

Most web applications now use the classic three-tier hierarchical structure, so it is best to use @Repository, @Service and @Controller in the persistence layer, business layer and control layer to annotate the classes in the layer, respectively. Annotate neutral classes with @Component. In a slightly larger project, there are usually hundreds of components. If these components are configured with xml bean definitions, it will obviously increase the size of the applicationContext.xml configuration file, and it is not very convenient to find and maintain. Spring 2.5 introduces an automatic component scanning mechanism for us, which can find classes annotated with @Component, @Service, @Controller, @Repository annotations in the classpath, and incorporate these classes into the spring container for management. Its role is the same as configuring the component using the bean node in the applicationContext.xml file. To use the automatic scanning mechanism, we need to open the following configuration information (here also added references to beans, aop, tx (transactions)):

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- Using scanning + annotation for development can improve development efficiency, later maintenance becomes difficult, and readability deteriorates -->
	<context:component-scan base-package="com.study.persistent.services" />
	......
</beans>

 Where base-package is the package to be scanned (including all sub-packages), @Service is used to mark business layer components, that is, to define a bean, and automatically instantiate a bean with a lowercase letter according to the class name of the bean, such as Chinese The instantiation is Chinese, if you need to change the name yourself: @Service ("the bean name you changed by yourself"), @Controller is used to mark control layer components (such as actions in struts), @Repository is used to mark data access components, That is, DAO components, and @Component generally refers to components. When components are not well classified, we can use this annotation to mark them.

Injection method:
inject the DAO implementation class into the service implementation class, inject the service interface (not the service implementation class) into the action, do not new the injected class when injecting, because spring will inject it automatically, if you manually re-inject it. If new, there will be an error. Then the getter() and setter() methods are no longer needed after adding @Autowired to the property, and spring will also inject it automatically.
Note:
In the spring configuration file, you only need to add <context:annotation-config/> and <context:component-scanbase-package="package where the class needs to be injected"/>, you can use base-package="* "Indicates all classes.

Mark the @Autowired and @Qualifier annotations in front of the interface so that the interface can be injected by the container. When there are two implementation classes in the interface, you must specify one of them to inject, and use the lowercase string of the implementation class to inject, such as:

public class TestXxx {
@Autowired      
@Qualifier("chinese")       
private Man man;
......
}

 Some examples of spring annotations:

//@Service service layer components, used to mark business layer components
@Service
public class TestServiceImpl implements ITestService {
	......
}

 

//@Repository persistence layer component, used to mark data access components, namely DAO components
@Repository
public class TestDaoImpl implements ITestDao {
	......
}

 Among them, ITestService and ITestDao are interfaces, and the default name of getBean is the class name (the first letter is lowercase).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326107505&siteId=291194637