gpt4 book ai didi

java - Spring 中的 @Service 与 @Component

转载 作者:行者123 更新时间:2023-12-01 13:59:57 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What's the difference between @Component, @Repository & @Service annotations in Spring?

(29 个回答)


2年前关闭。




我了解@Component 和@Controller、@Component 和@Repository 之间的区别,但无法找到与@Component 相比我们在@Service 中获得的附加功能。

最佳答案

enter image description here

我们可以直接使用@Component对于每个 bean,但为了更好地理解和维护大型应用程序,我们使用 @Controller, @Service, @Repository .

@Component: generic stereotype for any Spring-managed component 
@Service: stereotype for service layer

@组件

@Controller、@Service 和@Repository 注释的定义告诉@Service 是一种特殊类型的@Component。特殊类型的注释也会被扫描,因为它们本身是用 @Component 注释的。注释,这意味着它们也是 @Component s。如果我们定义自己的自定义注解并使用 @Component 进行注解,它也会被扫描 <context:component-scan>
@Component
public @interface Service {
….
}

@Component
public @interface Repository {
….
}

@Component
public @interface Controller {

}

@服务
@Service bean 在存储库层中保存业务逻辑和调用方法。

关于java - Spring 中的 @Service 与 @Component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56260106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com