Search code examples
javaspring-bootaop

I want to make a aspect which will take effect when i add a annotation on the type and method,but failed


I want to make a aspect which will take effect when i add a annotation on the type and method. the Aspect class:

@Component
@Aspect
public class ActionUnlockAspect {
    @Before("@target(com.example.demo.aspect.ActionUnlockCheck)||@annotation(com.example.demo.aspect.ActionUnlockCheck)")
    public void check(JoinPoint joinPoint) {
         //do something...
    }

}

annotation class

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Repeatable(ActionUnlockChecks.class)
public @interface ActionUnlockCheck {
    int actionType();
}

I use springboot version 2.1.7.RELEASE. When i start the springboot project,error occur.here is the stacktrace:

Caused by: java.lang.IllegalArgumentException: Cannot subclass final class com.sun.jmx.mbeanserver.JmxMBeanServer
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:660) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy.generate(ClassLoaderAwareGeneratorStrategy.java:57) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:358) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.3.4.jar:5.3.4]
    at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_251]
    at java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_251]
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:572) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:419) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57) ~[spring-aop-5.3.4.jar:5.3.4]
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:206) ~[spring-aop-5.3.4.jar:5.3.4]
    ... 102 common frames omitted

Solution

  • It looks like your pointcut is too global. As @target() can only be evaluated during runtime, as is documented, the aspect is basically applied to all classes. One way is to use @within() instead. If that does not work, you can also add something like ... && within(my.own.app..*) in order to limit the aspect scope.