2011-03-20 64 views
1

我在我的类中使用@Aspect注释定义了一个切入点。JPA entityManager在Pointcut中为null

我配置使用我在我的上下文定义了自定义注释的切入点:

<aop:aspectj-autoproxy proxy-target-class="true"/> 
<!-- Messaging pointcut --> 
<bean id="messagePointcut" class="com.adobe.codex.aspects.MessagePointcut" > 
    <constructor-arg ref="msgPointcutEntityFactory"/> 
    <property name="buildDao" ref="buildDao"/> 
</bean> 


<!-- enable our own annotation --> 
<aop:config proxy-target-class="true"> 
    <aop:aspect ref="messagePointcut"> 
     <aop:pointcut id="proxiedMethods" expression="@annotation(com..codex.aspects.annotation.MessageGateway)"/> 
     <aop:around pointcut-ref="proxiedMethods" method="interceptAnnotatedMethod"/> 
    </aop:aspect> 
</aop:config> 

遗憾的是里面buildDao EntityManager的总是空,如果我有我的切入点buildDao参考。

不知道什么是解决这个问题的最好方法。

我假设问题是使用的编织(加载时间)不知道如何从entityManagerFactory bean创建一个entityManager。

这里是我的dao上下文的片段。

<context:annotation-config /> 
<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaProperties"> 
     <util:properties 
      location="classpath:com//codex/dao/jpa/hibernate.properties" /> 
    </property> 
</bean> 

<bean id="buildDao" class="com..codex.dao.jpa.JpaBuildDao"> 
    <description> 
     A DAO for Builds. 
    </description> 
    <property name="queryHelper" ref="queryHelper" /> 
    <property name="partDao" ref="partDao" /> 
    <property name="buildQueryFactory" ref="buildQueryFactory" /> 

</bean>  

这里是我的切入点:

@Aspect @Transactional() 公共类MessagePointcut实现有序,MsgObservable {

private MsgPointcutEntityFactory msgEntityFactory; 
private BuildDao buildDao; 


public void setBuildDao(BuildDao buildDao) { 
    this.buildDao = buildDao; 
} 



public MessagePointcut(MsgPointcutEntityFactory msgEntityFactory){ 
    this.msgEntityFactory = msgEntityFactory; 
} 

@Transactional(readOnly = true) 
public Object interceptAnnotatedMethod(ProceedingJoinPoint pjp) { 
    Object returnedEntity = null; 
    Object originalEntity = null; 



    try { //  

     // do stuff before executing the call 
     originalEntity = msgEntityFactory.fetch(id, Build.class); 

     //execute the call 
     returnedEntity = pjp.proceed(); 

     // do stuff after executing the call 
     // ... 

    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
    return returnedEntity; 
} 

@Override 
public int getOrder() { 
    return 2; 
} 

}

而且吾道的片段

@Repository 公共类JpaBuildDao实现BuildDao {

private static final Log log = LogFactory.getLog(JpaBuildDao.class); 

@PersistenceContext 
private EntityManager entityManager; 

private QueryHelper queryHelper; 
private BuildQueryFactory standardQueryFactory; 
private PartDao partDao; 

public Build getFlatBuild(Integer id) { 
    Build returnBuild; 

     Query query = entityManager.createQuery(
       "SELECT b FROM Build b " + 
       "WHERE " + 
       "b.id = :id"); 
     query.setParameter("id", id); 
     returnBuild = (Build) query.getSingleResult(); 


    return returnBuild; 
} 

回答

1

取得了一些进展。真正的问题是buildDao被原始注入切入点而不是实例化entityManager所需的Jpa代理。

原来只有当另一个配置细节进入混合时才会出现此问题。我也有两个实例的MethodInvokingFactoryBean豆注射到我的切入点:

<bean id="registerListenerJms" 
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
    <property name="targetObject"> 
     <ref local="messagePointcut" /> 
    </property> 
    <property name="targetMethod"> 
     <value>registerObserver</value> 
    </property> 
    <property name="arguments"> 
     <list> 
      <ref bean="jmsGateway" /> 
     </list> 
    </property> 
</bean> 

<bean id="registerListenerAmf" 
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
    <property name="targetObject"> 
     <ref local="messagePointcut" /> 
    </property> 
    <property name="targetMethod"> 
     <value>registerObserver</value> 
    </property> 
    <property name="arguments"> 
     <list> 
      <ref bean="amfGateway" /> 
     </list> 
    </property> 
</bean> 

当我删除这两个豆类我切入点没有得到原始的代理,但它得到一个JdkDynamicAopProxy与对刀基准。

不知道为什么MethodInvokingFactoryBean混乱注入道,但它确实。

底线是暂时我删除实现我的观察者模式,并与希望在勾豆切入点的依赖生活的MethodInvokingFactoryBean。

不是一个完整的解决方案,但在可接受解决方法。