Announcement Announcement Module
Collapse
No announcement yet.
How to make a DAO be a @Repository without using the annotation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to make a DAO be a @Repository without using the annotation?

    I have a set of DAO classes that I would normally add the @Repository annotation to, but I find it makes more sense to manually define the beans using these DAO classes in the application context. One reason is that I have to define several instances of one of the DAO classes, with some different parameters specified in the application context.

    As far as I can tell, the only functional difference I would get with the @Repository annotation is the translation of SQL exceptions. That might be a good thing, so I'd like to know whether it's practical to "manually wire" a DAO bean defined in the context to do the exceptino translation. I didn't see an obvious way to do this in the Spring doc.

  • #2
    In xml you can do the following

    Code:
    <bean id=“persistenceExceptionInterceptor”
               class=“org.springframework.dao.support.            
                            PersistenceExceptionTranslationInterceptor”/>
    
    <aop:config>
        <aop:advisor pointcut=“execution(* *..*Repository.*(..))”
                              advice-ref=“persistenceExceptionInterceptor” />
    </aop:config>
    This will use AOP pointcut expression that you write to match your dao classes, then Spring will create a proxy to wrap around your daos with the PersistenceExceptionTranslationInterceptor added to the proxy. Basically the same end result as using @Repository and <bean class=“org.springframework.dao.annotation.Persiste nceExceptionTranslationPostProcessor”/>

    Hope that helps

    Mark

    Comment


    • #3
      Hmm, I added the following:
      Code:
          <bean id="persistenceExceptionInterceptor"
                class="org.springframework.dao.support.PersistenceExceptionTranslationInterceptor"/>
      
          <aop:config>
              <aop:advisor pointcut="execution(* mypackagepath.*.*(..))"
                           advice-ref="persistenceExceptionInterceptor" />
          </aop:config>
      The first thing that happened is that it failed to find the "BCException" class. This is defined in org.aspectj.aspectjweaver, so I added that dependency.

      Then, it failed with this:
      Caused by: java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.

      What am I missing?

      Comment

      Working...
      X