java Spring MVC 中的@Service 是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5142634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:40:01  来源:igfitidea点击:

What is @Service in Spring MVC

javaspring-mvc

提问by kosmoplan

If I use @Serviceon a service class, do I need to make a service class bean in my servlet xml file or do I have to do both?

如果我@Service在服务类上使用,我是否需要在我的 servlet xml 文件中创建一个服务类 bean 或者我必须两者都做?

回答by kosmoplan

You don't have to declare a bean in your context file if you:

如果您符合以下条件,则不必在上下文文件中声明 bean:

1) Annotate the class with:

1) 使用以下注释对类进行注释:

@Component, @Service, @Controller or @Repository

2) Include the context:component-scan element in your context file like this:

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

<context:component-scan base-package="your.package" />

Hope that helps.

希望有帮助。

回答by serg10

Last time I looked (Spring 2.5) @Servicewas a marker annotation subclassed from @Component, but with no additional behaviour. Which means that beans tagged with @Servicebecome candidates for auto detection if you are using annotation-based configuration via classpath scanning.

上次我看 (Spring 2.5) @Service是一个标记注释,从@Component类化,但没有额外的行为。这意味着,如果您通过类路径扫描使用基于注释的配置,则标记为@Service 的bean 将成为自动检测的候选对象。

As per the docs, the intention is that this annotation might include service layer specific functionality in future Spring releases. It can also act as an AOP point cut for all of your service layer components.

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