gpt4 book ai didi

spring - 使用来自 Spring Java 配置的 @Repository-style 异常转换

转载 作者:行者123 更新时间:2023-12-04 15:26:44 25 4
gpt4 key购买 nike

如果我想使用 Spring 3 的基于 Java 的配置声明一个 bean,我可以这样做:

@Configuration
public class MyConfiguration {
@Bean
public MyRepository myRepository() {
return new MyJpaRepository();
}
}

但是,因为我不能使用 @Repository这个上下文中的注解,如何让Spring进行异常翻译?

最佳答案

将 MyJpaRepository 类声明为存储库:

@Repository
public class MyJpaRepository {
...
}

并通过在 Spring 配置中设置 component-scan 元素来确保您的注释可被发现:
<context:component-scan base-package="org.example.repository"/>

由于您不希望根据您的评论将您的存储库包含在注释扫描中,请通过排除所有 @Repository 注释或您的特定类或包来过滤掉它。文档中有一个例子:
<context:component-scan base-package="org.example.repository">
<!-- use one or the other of these excludes, or both if you *really* want to -->
<context:exclude-filter type="regex" expression="*Repository"/>
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

Spring 3.0 documentation在 3.10 节中更详细地描述了这个配置。

通过将您的类配置为 Repository,当您在 Configuration 类中将其作为 Bean 拉出时,它将被指定为一个。

关于spring - 使用来自 Spring Java 配置的 @Repository-style 异常转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5745069/

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