繁体   English   中英

Spring AOP 未在 spring boot 2.1.6 应用程序中被调用

[英]Spring AOP not getting invoked in spring boot 2.1.6 application

在谷歌上关注了这么多关于这个主题的搜索结果后,我的 Aspect 在我的 Spring Boot 应用程序上仍然不起作用

我使用的是 spring boot 版本 2.1.6,它似乎已经有 spring aop、aspectjweaver 和 aspectjrt(待更正)。 我创建了一个注释、方面组件,并在目标类上使用了我的注释,但没有成功。

这是我的注释类

    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;

    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;

    @Retention(RUNTIME)
    @Target({TYPE, METHOD})
    public @interface AopAudit {
    }

我的方面课

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

    @Aspect
    @Component
    public class AuditAnnotationAspect {
        @Before("execution(* com.rainestech.hrm.modules.settings.entity.ABC.set*(..))")
        public void before(JoinPoint joinPoint) {
    System.out.println("Audit Works!!! = ");
        }
    }

ABC类

@Entity
@AopAudit
public class ABC {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotEmpty
    @NotNull
    @Column
    private String name;

    //... getters and setters
}

配置类

@Configuration
@EnableWebSecurity
@EnableAspectJAutoProxy
public class WebSecurity extends WebSecurityConfigurerAdapter {

}

在 ABC 类上运行应用程序和运行 set 方法不会产生任何效果,而我希望在控制台中看到 Audit Works

首先,确保您的pom.xml包含所有这些:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

其次,使用@EnableAspectJAutoProxy注释您的配置,这将启用它。

第三,确保你更新你的切入点:

@Pointcut("@annotation(com.path.to.your.annotation.AopAudit)")
private void auditable() {}

然后在你的@Before使用它。

@Before("auditable()")

另一个需要注意的重要事项是您不能执行位于同一类上的方法切入点。 更多信息在这里

只是一个非常愚蠢的评论,因为我花了一天 :-(。不要忘记 @Autowire 你是注释对象。AOP 通常只有在你通过 @Component 扫描上下文解析所有内容时才能工作!

//This will work : Resolve everything trough the @Component container context
@Autowired
FeatureAnnotationAspect aspspectTest;

@Test
public void testAnnotationIntersception() 
{
    log.debug("TestFeatureFlagAspect::testAnnotationIntersception");
   //This wont work! You need to Autowire the object to get all annotations and AOP working!
    //aspcetTest = new FeatureAnnotationAspect();
    aspectTest.Annotate(0);
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM