什么是Spring MVC中的@Service

12 java spring-mvc

如果我@Service在服务类上使用,我是否需要在servlet xml文件中创建服务类bean,还是必须同时执行这两个操作?

kos*_*lan 14

如果您执行以下操作,则不必在上下文文件中声明bean:

1)用以下内容注释类:

@Component, @Service, @Controller or @Repository
Run Code Online (Sandbox Code Playgroud)

2)在上下文文件中包含上下文:component-scan元素,如下所示:

<context:component-scan base-package="your.package" />
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.


ser*_*g10 5

上次我看(Spring 2.5)@Service@Component的标记注释,但没有其他行为.这意味着如果您通过类路径扫描使用基于注释的配置,则使用@Service标记的bean将成为自动检测的候选者.

根据文档,目的是这个注释可能包含未来Spring版本中特定于服务层的功能.它还可以充当所有服务层组件的AOP切入点.