0

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

mkyong

Spring AOP

bealdung

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!

5
  • The reason being the account instance you are creating is not a spring managed bean. You are using new Account() to create the instance. Instead get the Account bean from Spring context. Also I am not sure if you are creating a spring ApplicationContext instance . Are you using Spring boot ? Commented Jun 10, 2020 at 3:45
  • I think the main reason for application to not work as expected are becuase 1. The Spring Application context is not created 2. The Account instance should be spring bean for Spring AOP to work Commented Jun 10, 2020 at 3:53
  • No, I'm not creating the bean via ApplicationContext, I'll try that now. And I'm just using Spring, no Spring Boot. Commented Jun 10, 2020 at 3:55
  • Do you need a code sample ? I can share that as an answer to this question Commented Jun 10, 2020 at 3:56
  • That would be great if I can get a code sample! Commented Jun 10, 2020 at 3:57

1 Answer 1

1

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(); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You were right - the app context stuff completely flew over my head, thank you again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.