Javers:@annotation(org.javers.spring.annotation.JaversAuditable)在某个项目中不起作用

时间:2017-10-10 11:29:15

标签: java spring spring-mvc javers

我有两个项目,一个是真正的项目,第二个是尝试使用类似设置和类似环境的Javers Framework的演示项目。在演示项目中,Javers审计跟踪运行良好,但在实际项目中实现后,Javers无法正常工作。

我已经对这个过程进行了几天的调查并找到了这个假设 “实际项目中的Javers无法检测到切入点(更准确地说无法检测到注释)”:

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

因为当我使用相同的切入点创建一个简单的方面时:

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

方面是执行,当我尝试用另一个切入点击@annotation(org.javers.spring.annotation.JaversAuditable)时(@ 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());
    }
}

1 个答案:

答案 0 :(得分: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 Config)