Results 1 to 3 of 3

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

  1. #1
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default 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. #2
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •