@Service vs @Component在Spring中的区别

23 浏览
0 Comments

@Service vs @Component在Spring中的区别

这个问题已经有了答案:

Spring框架中@Component、@Repository和@Service注解有什么区别?

我理解@Component和@Controller、@Component和@Repository之间的区别,但是无法找到在@Service中相对于@Component有什么额外的特性。

admin 更改状态以发布 2023年5月22日
0
0 Comments

在此输入图片描述

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

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


@Component

@Controller、@Service和@Repository注解的定义表明@Service是@Compoent的一种特殊类型。特殊类型的注解也会被扫描,因为它们本身被注解了@Component注解,这意味着它们也是@Component。如果我们定义自己的自定义注解并用@Component进行注释,则它也将被扫描。

@Component
public @interface Service {
    ….
}
@Component
public @interface Repository {
    ….
}
@Component
public @interface Controller {
    …
}

@Service

@Servicebean保存业务逻辑并调用存储库层中的方法。

0