Reputation: 1965
I apologize for possibly repeating this problem (as I've seen in many other SO sites) however, I'm not sure what else to try at this point.
I am trying to setup a simple AspectJ Spring application. Once I have a working example, I would like to possibly create a common library that can be used within my team via annotations, so I'm very interested in using AspectJ as I think it could simplify a lot of what I'm trying to do.
Below is my code:
AspectDemoApplication.class - My main application start point. I'm initializing a new Account object and calling the method, hoping that I would see the advice for this method when run.
public class AspectDemoApplication {
public static void main(String[] args) {
Account acct = new Account();
acct.sayHello();
}
}
Account.class - The service class that holds the method.
public class Account {
public String sayHello() {
System.out.println("hello from method");
return "hello";
}
}
LogAspect.class - my aspect class where I've defined the pointcut and advice.
@Aspect
//@Component
public class LogAspect {
@Pointcut(value = "execution(* com.rob.aspectdemo.Account.sayHello(..))")
private void logBefore() { }
@Before("logBefore()")
public String print() {
System.out.println("before hello");
return null;
}
}
AspectJConfig.class - my AspectJ configuration class. Most online resources use .xml configurations but from the Javadocs on EnableAspectJAutoProxy, it says it serves as the same function.
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackages = "com.rob")
public class AspectJConfig {
@Bean
public LogAspect getLogAspect() {
return new LogAspect();
}
@Bean
public Account getAccount() {
return new Account();
}
}
build.gradle - my gradle file showing my dependencies (I can see I have the aspectjrt.jar in the external libs).
implementation 'org.springframework:spring-core:5.2.7.RELEASE'
implementation 'org.springframework:spring-context:5.2.7.RELEASE'
implementation 'org.springframework:spring-aop:5.2.7.RELEASE'
implementation 'org.aspectj:aspectjrt:1.9.5'
implementation 'org.aspectj:aspectjweaver:1.9.5'
implementation 'org.aspectj:aspectjtools:1.9.5'
My problem is, when I start the application (calling the main() method), it is able to print the println statement within the Account.sayHello() method but it does not print the println in the advice.
I've tried using these online guides (and many other SO links) but to no avail (most sites state the same thing):
I'm am using IntlliJ v2020.1 and even in this IDE, I have a symbol next to my Before advice where it says "This advice advises no methods". But when I use the IDE's PointcutExpression Fragment tool, it says the pointcut syntax is correct.
Any help would be greatly appreciated! Thank you!
Upvotes: 0
Views: 1210
Reputation: 7121
Following code will create a Spring application context and get the account bean from the context
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AspectDemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AspectJConfig.class);
ctx.registerShutdownHook();
Account acct = ctx.getBean(Account.class);
acct.sayHello();
}
}
Upvotes: 1