Skip to content
Advertisement

Spring AOP not getting invoked in spring boot 2.1.6 application

After following so many search results on google on this subject, my Aspect doesn’t work still on my spring boot application

I am using spring boot version 2.1.6, which seem to have already spring aop, aspectjweaver and aspectjrt (stand to be corrected). i created an annotation, aspect component and use my annotation on a target class with no success.

here is my annotation class

    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 {
    }

my aspect class

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!!! = ");
        }
    }

class ABC

@Entity
@AopAudit
public class ABC {

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

    @NotEmpty
    @NotNull
    @Column
    private String name;

    //... getters and setters
}

configuration class

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

}

running the application and running set method on class ABC produce no effect whereas i expect to see Audit Works in the console

Advertisement

Answer

First, make sure your pom.xml contains all of these:

<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>

Second, annotate your configuration with @EnableAspectJAutoProxy, this will enable it.

Third, make sure you update your pointcut:

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

And then use it in your @Before.

@Before("auditable()")

Another important thing to notice is that you cannot execute a method pointcut of which is located on the same class. More info here.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement