2017-10-10 29 views
0

我有兩個項目,一個是真正的項目,第二個是一個示範項目嘗試Javers框架具有類似的設置和類似的環境。在演示項目中,Javers的審計線索運行良好,但是在實際項目中實施後,Javers無法工作。Javers:@annotation(org.javers.spring.annotation.JaversAuditable)在一些項目不工作

我已經調查的過程中好幾天,發現這個假設 「Javers在實際項目中無法檢測到切入點(更精確地檢測不到標註)」:

@AfterReturning("@annotation(org.javers.spring.annotation.JaversAuditable)")(在JaversAuditableAspect)

因爲當我創建一個具有相同切入點簡單的方面:

@AfterReturning("execution(* com.xxx.StatusRepository.*(..))") 

的方面是執行,而當我試着打@annotation(org.javers.spring.annotation.Ja versAuditable)與另一切入點(@Before,@After,@AfterThrowing和@Around)沒有一個切入點被擊中。

是否有任何其他線索或選項,這樣我可以試試嗎?

這裏是我的xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    "> 

    <tx:annotation-driven/> 

    <import resource="classpath:META-INF/spring/mbp-infra.xml" /> 
    <import resource="classpath:META-INF/spring/mbp-util.xml" /> 
    <import resource="classpath*:META-INF/spring/**/*-codelist.xml" /> 

    <context:component-scan base-package="com.nttdata.mbp.domain" /> 

    <!-- AOP. --> 
    <bean id="resultMessagesLoggingInterceptor" 
     class="org.terasoluna.gfw.common.exception.ResultMessagesLoggingInterceptor"> 
      <property name="exceptionLogger" ref="exceptionLogger" /> 
    </bean> 
    <aop:config proxy-target-class="true"> 
     <aop:advisor advice-ref="resultMessagesLoggingInterceptor" 
        pointcut="@within(org.springframework.stereotype.Service)" /> 
    </aop:config> 

    <aop:aspectj-autoproxy/> 

    <!-- check that the aop is running --> 
    <bean id = "myAspect" class = "com.nttdata.mbp.domain.util.AOPTest" /> 

</beans> 

這裏是我的依賴關係:

<!-- == Javers == --> 
<dependency> 
    <groupId>org.javers</groupId> 
    <artifactId>javers-core</artifactId> 
    <version>3.5.1</version> 
</dependency> 

<dependency> 
    <groupId>org.javers</groupId> 
    <artifactId>javers-spring</artifactId> 
    <version>3.5.1</version> 
</dependency> 

<dependency> 
    <groupId>org.javers</groupId> 
    <artifactId>javers-persistence-jdbc</artifactId> 
    <version>1.0.3</version>  
</dependency> 

<dependency> 
    <groupId>org.javers</groupId> 
    <artifactId>javers-persistence-sql</artifactId> 
    <version>3.5.1</version> 
</dependency> 

<dependency> 
    <groupId>org.polyjdbc</groupId> 
    <artifactId>polyjdbc</artifactId> 
    <version>0.6.4</version> 
</dependency> 
<!-- == End Javers == --> 

===更新===

這裏是我的javers bean配置

@Configuration 
public class AuditContext { 

    @Bean 
    public Javers javers() { 
     JaversRepository javersRepository = SqlRepositoryBuilder 
              .sqlRepository() 
              .withConnectionProvider(getConnectionProvider()) 
              .withDialect(DialectName.MYSQL).build(); 
     return JaversBuilder.javers() 
       .registerJaversRepository(javersRepository) 
       .build(); 
    } 

    @Bean 
    public ConnectionProvider getConnectionProvider() { 
     return new AuditConnectionProvider(); 
    } 

    @Bean 
    public AuthorProvider getAuthorProvider() { 
     return new SpringSecurityAuthorProvider(); 
    } 

    @Bean 
    public CommitPropertiesProvider getCommitPropertiesProvider() { 
     return new CommitPropertiesProvider() { 
      @Override 
      public Map<String, String> provide() { 
       return ImmutableMap.of("key", "ok"); 
      } 
     }; 
    } 

    @Bean 
    public JaversAuditableAspect javersAuditableAspect() { 
     return new JaversAuditableAspect(javers(),getAuthorProvider(),getCommitPropertiesProvider()); 
    } 

    @Bean 
    public JaversSpringDataAuditableRepositoryAspect javersSpringDataAuditableAspect() { 
     return new JaversSpringDataAuditableRepositoryAspect(javers(),getAuthorProvider(),getCommitPropertiesProvider()); 
    } 
} 

回答

0

https://javers.org/documentation/spring-integration/ 描述所有你需要做的就是通過聲明這個bean,使JaVers'方面:

@Bean 
public JaversAuditableAspect javersAuditableAspect() { 
    return new JaversAuditableAspect(javers(), authorProvider(), commitPropertiesProvider()); 
} 

並啓用@AspectJ支持。

Spring配置的完整例子可以在這裏找到https://javers.org/documentation/spring-integration/#spring-jpa-example (我們沒有在XML中的例子,我建議升級到Java配置)

+0

對不起,我不提它,我已經更新了我的文章與配置類,我也檢查了註釋'System.out.println(target.getClass()。getMethod(「insert」,Case.class).isAnnotationPresent(JaversAuditable.class));'和結果是真實的,但它仍然沒有工作,我不知道了。 – Richant

+0

幾天後,我和我的朋友在尋找這個問題,今天上午,他來到辦公室,終於找到了原因。這是因爲我們團隊中的某個人將spring-mvc.xml文件中的組件掃描從''>更改爲'',只是丟失了「.app」這個詞。我們不關注這個文件,因爲一切正常,我們也沒有「.app」包。直到這個簡單的不同導致問題@annotation切入點(感謝Javers,我們從它那裏得到一個新的教訓)。 – Richant

相關問題