2011-03-20 97 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。

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