【问题标题】:@Service vs @Component in Spring [duplicate]Spring中的@Service与@Component [重复]
【发布时间】:2019-10-09 03:15:48
【问题描述】:

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

【问题讨论】:

标签: java spring spring-boot spring-mvc annotations


【解决方案1】:

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

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

@Component

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

@Component
public @interface Service {
    ….
}

@Component
public @interface Repository {
    ….
}

@Component
public @interface Controller {
    …
}

@Service

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

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 2011-10-13
    • 2021-02-21
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多