Reputation: 111
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
Upvotes: 1
Views: 2204
Reputation: 2571
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.
Upvotes: 1
Reputation: 1083
Just a very silly remark, as it took me a day :-(. Dont forget to @Autowire youre annotated objects. AOP will typically only work if you resolve everything by the @Component scanned context!
//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);
}
Upvotes: 0