Spring AOP:AopConfigException:无法生成类

时间:2017-12-04 06:22:40

标签: java spring spring-aop

以下是我的LoggingAspect.java

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class LoggingAspect {

    private static Logger logger = Logger.getLogger(LoggingAspect.class.getName());
    private static final String ENTERING = "(AOP) Entering ";
    private static final String EXITING = "(AOP) Exiting ";
    private static final String RETURNING = "(AOP) Returning for class ";
    private static final String EXCEPTION = "(AOP): Exception ";

    @Pointcut("execution(* com.adminserver..*(..)) && " + "!within(is(EnumType)) && " + "!within(is(FinalType))")
    protected void loggingOperation() {
        // intentionally left blank
    }

    private String getArguments(Object[] obj) {
        String str = "{";
        for (int i = 0; i < obj.length && obj[i] != null; i++)
            str += obj[i].toString() + ",";
        str += "}";
        return str;
    }

    @Before("loggingOperation()")
    public void logEntry(final JoinPoint joinPoint) {
        String msg = ENTERING + joinPoint.getTarget().getClass().getName() + " " + joinPoint.getSignature().getName()
                + " Arguments: " + getArguments(joinPoint.getArgs());

        logger.log(Level.INFO, msg);

    }

    @After("loggingOperation()")
    public void logExit(final JoinPoint joinPoint) {
        logger.log(Level.INFO, EXITING + joinPoint.getTarget().getClass().getName() + " "
                + joinPoint.getSignature().getName() + " Arguments:" + getArguments(joinPoint.getArgs()));
    }

    @AfterReturning(pointcut = "loggingOperation()", returning = "result")
    public void logReturn(final JoinPoint joinPoint, Object result) {
        String msg = RETURNING + joinPoint.getTarget().getClass().getName() + ", Method: "
                + joinPoint.getSignature().getName() + " Return Value: ";
        if (result != null)
            logger.log(Level.INFO, msg + result.toString());
        else
            logger.log(Level.INFO, msg + "NULL");

    }

    @AfterThrowing(pointcut = "loggingOperation()", throwing = "ex")
    public void logException(JoinPoint joinPoint, Throwable ex) throws Throwable {
        String msg = EXCEPTION + joinPoint.getTarget().getClass().getName() + " " + joinPoint.getSignature().getName();
        logger.log(Level.SEVERE, msg, ex);
    }

}

尝试使用以下两个配置(在SO上搜索答案后):

<aop:aspectj-autoproxy proxy-target-class="true"/><aop:aspectj-autoproxy proxy-target-class="false"/>

应用程序正在运行,它正在按预期运行代码的某些部分。但是对于其他一些部分,它会抛出这样的东西:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'onLoadProcessor_PolicyOverviewScreen_b155c215e3379a2caad2916f665ce44': Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.adminserver.sre.bll.generatedClasses.OnLoadProcessor_PolicyOverviewScreen_b155c215e3379a2caad2916f665ce44]: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
    at com.adminserver.utl.SpringBeanUtl.getBean(SpringBeanUtl.java:55)
    at com.adminserver.sre.helper.generator.OnLoadGenerator.generate(OnLoadGenerator.java:109)
    at com.adminserver.sre.helper.generator.OnLoadGenerator.getOnLoadProcessorBll(OnLoadGenerator.java:58)
    at com.adminserver.sre.helper.generator.OnLoadGenerator.getOnLoadProcessorBllForProcessing(OnLoadGenerator.java:35)
    at com.adminserver.sre.bll.ScreenEventBll.processLoad(ScreenEventBll.java:130)
    ... 124 more
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.adminserver.sre.bll.generatedClasses.OnLoadProcessor_PolicyOver+viewScreen_b155c215e3379a2caad2916f665ce44]: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:205)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:469)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:349)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    ... 135 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345)
    at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
    at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
    at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:55)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:201)
    ... 142 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor31.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:459)
    at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
    ... 155 more
Caused by: java.lang.NoClassDefFoundError: com/adminserver/sre/bll/generatedClasses/OnLoadProcessor_PolicyOverviewScreen_b155c215e3379a2caad2916f665ce44
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    ... 160 more
Caused by: java.lang.ClassNotFoundException: com.adminserver.sre.bll.generatedClasses.OnLoadProcessor_PolicyOverviewScreen_b155c215e3379a2caad2916f665ce44
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
    ... 162 more

使用了lib:

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

正如您所看到的,我使用

排除了所有枚举类型和最终类

@Pointcut("execution(* com.adminserver..*(..)) && " + "!within(is(EnumType)) && " + "!within(is(FinalType))")

但是在日志中它仍然会抱怨Common causes of this problem include using a final class or a non-visible class;

1 个答案:

答案 0 :(得分:0)

在切入点表达式中添加!within(com.adminserver..*.generatedClasses..*)并且它有效。

以下是完整的表达。

@Pointcut("execution(public * com.adminserver..*(..)) && " + "!within(is(EnumType)) && " + "!within(is(FinalType)) && "+"!within(com.adminserver..*.generatedClasses..*)")
    protected void loggingOperation() {
        // intentionally left blank
    }