Search code examples
springaspectjspring-aopaspectj-maven-plugin

java.lang.IllegalStateException: Per-clause not recognized: pmAspect


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

Solution

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