如果我想使用Spring 3的基于Java的配置声明bean,我可以这样做:
@Configuration
public class MyConfiguration {
@Bean
public MyRepository myRepository() {
return new MyJpaRepository();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,由于我不能@Repository在此上下文中使用注释,如何让Spring执行异常转换?
将您的 MyJpaRepository 类声明为存储库:
@Repository
public class MyJpaRepository {
...
}
Run Code Online (Sandbox Code Playgroud)
并确保通过在 Spring 配置中设置 component-scan 元素来发现注释:
<context:component-scan base-package="org.example.repository"/>
Run Code Online (Sandbox Code Playgroud)
由于您不希望您的存储库包含在根据您的注释进行的注释扫描中,因此可以通过排除所有 @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>
Run Code Online (Sandbox Code Playgroud)
Spring 3.0 文档第 3.10 节更详细地描述了此配置。
通过将您的类配置为存储库,当您将其作为配置类中的 Bean 拉出时,它将被指定为存储库。
| 归档时间: |
|
| 查看次数: |
1416 次 |
| 最近记录: |