【问题标题】:Spring - Why do we need mark a class @repository(or whatever other than @Component) [duplicate]Spring - 为什么我们需要标记一个类@repository(或@Component以外的任何东西)[重复]
【发布时间】:2015-06-21 18:26:41
【问题描述】:

我正在努力学习Spring in Detail,所以在我的示例中我有一个DAO

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
    ...
}

它工作正常,但@Component 也一样工作正常,没有任何问题

@Component
public class EmployeeDAOImpl implements EmployeeDAO {
    ...
}

那么我为什么要标记我的 具有@Repository 的类,除了持久层的刻板印象之外还有其他优势吗?

【问题讨论】:

  • 我永远不会同意downvoter,当我们用@Repository注释一个类时,我们会得到更多的东西,让我找到答案
  • 当与 org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor PersistenceExceptionTranslationPostProcessor 结合使用时,带有 @Repository 注释的类符合 Spring DataAccessException 转换的条件。

标签: java spring spring-mvc


【解决方案1】:

Repository的源码那也是Component

@Component
public @interface Repository { ... }

它只是为了明确组件/bean的用途。

同样ServiceController 也是用于服务和控制器的Component

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2013-12-17
    • 2019-09-17
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多