The use of spring annotation @Service annotation

 To illustrate the use of the @Service annotation, we have to say that we often see the configuration in the following figure in the spring configuration file applicationContext.xml:

<!-- Using scanning + annotation for development can improve development efficiency, later maintenance becomes difficult, and readability deteriorates -->
<context:component-scan base-package="com.study.persistent" />

 After adding this line to the applicationContext.xml configuration file, the package under the specified path will be automatically scanned. If a class is annotated with @Service, it will be automatically registered to the Spring container, and there is no need to define beans in the applicationContext.xml configuration file. Yes, and similar ones include @Component, @Repository, and @Controller.

Such as this class:

@Service("courseDAO")
@Scope("prototype")
public class CourseDAOImpl extends HibernateDaoSupport implements CourseDAO{
......
}

 Its role is equivalent to configuring the following information in the applicationContext.xml configuration file:

<bean id="courseDAO"
      class="com.study.persistent.CourseDAOImpl" scope="prototype">
      ......    
</bean>

 The @Service("serviceName") annotation is equivalent to the <bean id="serviceName"> configured in the applicationContext.xml configuration file, indicating that the current class is named an alias, which is convenient for injection into other classes that need to be used. The @Service annotation can also not specify serviceName. If you do not specify it is equivalent to <bean id="com.study.service.serviceName">, com.study.service.ServiceName is the fully qualified name of this class. If it is not added, the default alias It is the current class name, but the first letter is lowercase.

Guess you like

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