开发者

Using @Repository-style exception translation from Spring Java configuration

开发者 https://www.devze.com 2023-02-28 10:16 出处:网络
If I want to declare a bean using Spring 3\'s Java-based configuration, I can do this: @Configuration public class MyConfiguration {

If I want to declare a bean using Spring 3's Java-based configuration, I can do this:

@Configuration
public class MyConfiguration {
    @Bean
    public MyRepository myRepository() {
   开发者_开发百科     return new MyJpaRepository();
    }
}

But, since I can't use the @Repository annotation in this context, how do get Spring to perform exception translation?


Declare your MyJpaRepository class as a repository:

@Repository
public class MyJpaRepository {
  ...
}

And make sure you have your annotations discoverable by setting up the component-scan element in your Spring configuration:

<context:component-scan base-package="org.example.repository"/>

Since you do not want your repository included in the annotation scan per your comments, filter it out either by excluding all @Repository annotations or your particular class(es) or package. There is an example of this in the documentation:

<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>

The Spring 3.0 documentation describes this configuration in more detail in section 3.10.

By configuring your class as a Repository, it will be designated as one when you pull it out as a Bean in your Configuration class.

0

精彩评论

暂无评论...
验证码 换一张
取 消