java - Spring 中的 @Service 与 @Component

标签 java spring spring-boot spring-mvc annotations

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





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/

相关文章:

java - 如何使用当前日期的名称创建一个新文件

java - Spring Security oAuth 提示缺少 JWT 验证程序 key ,即使它已定义

spring - 自动为每个响应添加标题

java - 如何禁用 Spring Actuators 的内容协商?

java - Spring Boot 将 yaml 列表加载为 map

java - 为什么没有碰撞?

java - 将多个不同类型的参数传递给 doInBackground (Params...params) 方法的最正确方法是什么?

java - 添加背景图像到 JPanel

mysql - 如何使用hibernate +Spring编写更新查询

java - 为什么这个 Spring Boot/Thymeleaf 按钮会隐藏我的数据或根本不起作用?