Results 1 to 3 of 3

Thread: aspectj aspects and tx annotation driven

  1. #1
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default aspectj aspects and tx annotation driven

    Hi everyone,

    I hope you can help me with this one:

    I'm writing JUnit tests for a JPA 2.0 application. As you may know, when writing transactional tests, you need to manually flush the entitymanager after every service call (which are annotated with @Transactional as well) (otherwise changes in the unit test are propagated to the entitymanager due to the open persistence context, causing all kind of mayhem when doing multiple calls and changing entities in the same test). Since I do not want to inject the entitymanager in my tests (my tests shouldn't have to know I'm using JPA, they're testing services), I've written an @AspectJ aspect that flushes the EM after every transactional service cal, as all services extends from an abstract base class which has the @Transactional annotation.
    It looks like this:
    Code:
    @Aspect
    public class DetachEMAfterTransactionalCallAspect {
    ...
        @Around("@target(org.springframework.transaction.annotation.Transactional)")
        public Object doDetach(ProceedingJoinPoint methodInvocation) {
            ...
            getEntityManager().flush();
            ...
        }
    ...
    }
    So my test XML config looks like this (services are in an @Configuration class):
    Code:
    <beans ...>
         <context:annotation-driven/>
         <context:spring-configured/>
         <tx:annotation-driven proxy-target-class="true"/>
         <bean id="servicesConfig" class="org.example.services.ServicesConfig"/>
         <bean id="detachAspect" class="org.example.unittesting.DetachEMAfterTransactionalCallAspect "/>
         <aop:aspectj-autoproxy/>
    </beans>
    Unfortunately, my aspect is never called. It looks like the TX aspect changes the objects in such a way that the services no longer contain the @Transactional annotation and because of this, the detach aspect is never called... I though the proxy-target-class made the tx proxy retain all information of the originating class?

    Any ideas?
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  2. #2
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    Found a solution: don't use aspectj-autoproxy, but define the aop config instead.

    Code:
    <aop:config>
        <aop:pointcut id="transactionalPointcut" expression="@target(org.springframework.transaction.annotation.Transactional)"/>
        <aop:aspect id="detachAopAspect" ref="detachAspect">
            <aop:around method="doDetach" pointcut-ref="transactionalPointcut">
        </aop:aspect>
    </aop:config>
    Works... Still don't know why the AspectJ didn't work tho, but since this method works I'm not really bothering to look into it.
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  3. #3
    Join Date
    Oct 2010
    Posts
    10

    Default

    How did you manage to get the entity manager in the Aspect? I have tried @Autowired, using the @PersistenceContext and passing the em factory as a property in the aspect bean definition in my application context xml. All result in null. I want to do a similar thing to you, call em.flush after a point cut, all seems to work apart from getting at the entity manager. Any advice would be great.

    Thanks.

Posting Permissions

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