1

I have a problem with the <aop:aspectj-autoproxy/> configuration tag.

I have added aspectjrt.jar and aspectjweaver.jar, but my setup still is not working.

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.9</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.1</version>
</dependency>
org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ser' defined in file \[C:\\Users\\kunar\\eclipse-workspace\\Spring\\AopProj1\\target\\classes\\com\\krk\\service\\ServiceTest.class\]: BeanPostProcessor before instantiation of bean failed
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ser' defined in file \[C:\\Users\\kunar\\eclipse-workspace\\Spring\\AopProj1\\target\\classes\\com\\krk\\service\\ServiceTest.class\]: BeanPostProcessor before instantiation of bean failed
Caused by: java.lang.IllegalStateException: Per-clause not recognized: pmAspect
    at org.aspectj.internal.lang.reflect.AjTypeImpl.getPerClause(AjTypeImpl.java:183)
    at org.springframework.aop.aspectj.annotation.AspectMetadata.\<init\>(AspectMetadata.java:103)
@Component
@Aspect("pmAspect") 
public class AspectTest {
  @Around(value = "execution(int com.krk.service.ServiceTest.a*(..))")
  public Object around(ProceedingJoinPoint php) throws Throwable {
    System.out.println("before method call");
    Object ob=php.proceed();
    System.out.println("after method call");
    return ob;
  }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Welcome to Stack Overflow. Please learn [how to ask a question on SO](https://stackoverflow.com/help/how-to-ask) and provide a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). Feel free to post it on GitHub, then edit your question, linkingto it. Please also be a bit less sloppy in formatting your code, making the question look less chaotic and more readable. Then I will be glad to help you. Thank you. – kriegaex Jul 29 '23 at 05:01
  • Besides, why would you need AspectJ Maven Plugin for Spring AOP? Why did you select that tag for your question? And why are you using different versions - both very outdated - for AspectJ weaver and runtime in your Maven POM? – kriegaex Jul 30 '23 at 01:40
  • P.S.: I just completely reworded and reformatted your question to make reading it hurt other people's eyes a bit less. – kriegaex Jul 30 '23 at 01:41

1 Answers1

0

The way you use @Aspect("pmAspect") is obviously wrong. It seems as if you want to name the aspect,but you should do that in @Component instead, or possibly in @Bean, if for any reason in another case you want to use a factory method in your configuration class.

If you look at the @Aspect javadoc, you will notice that the value parameter is not for naming but for specifying an instantiation type, if you need something other than a singleton aspect. You cannot just put anything into a string parameter and simply assume that it will be interpreted as a name or label.

Therefore, the solution to your problem is to simply use @Aspect without any parameters.

One more thing: You do not need both AspectJ weaver and runtime in your POM. The weaver already contains the runtime. And you should upgrade to a more recent AspectJ version, not just copy and paste random snippets from the WWW. The latest version would be:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.19</version>
</dependency>

But that is not the root cause of your problem.

kriegaex
  • 63,017
  • 15
  • 111
  • 202