在课堂上使用AOP和最终成员

时间:2013-05-01 14:40:12

标签: spring aop aspectj spring-aop cglib

我正在尝试使用spring aop。 我配置了以下方面:

@Component
@Aspect
public class BenchmarkAspect {
    private static final Logger logger = LoggerFactory.getLogger(BenchmarkAspect.class);

    @Around(value = "@annotation(benchmark)")
    public void do(final ProceedingJoinPoint joinPoint, final Benchmark benchmark)            throws Throwable {

    logger.error("Before");
    joinPoint.proceed();
    logger.error("After");
}

这是使用我的服装注释的课程的一个例子

@Component
public class AttributeContainer {
    private static final Logger logger = LoggerFactory.getLogger(AttributeContainer.class);
    private final int y;

    public AttributeContainer(int y){
          this.y = y;
    }

    @Benchmark
    public void getAttribute() {
         logger.error("Inside Attribute call...");
    }
}

当我加载我的应用程序时,我得到以下异常:

nested exception is org.springframework.aop.framework.AopConfigException: Could not  generate CGLIB subclass of class [class   com.test.AttributeContainer]: Common causes of this problem   include using a final class or a non-visible class; nested exception is   java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments   were given:
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:721)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:201)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1461)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)

更新的 添加我的spring config xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

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

  <context:component-scan base-package="com.test.*" />

  <bean id="test" class="com.test.AttributeContainer">
    <constructor-arg value="34"/>
  </bean>
</beans>

为什么CGlib无法创建子类? 谢谢!

2 个答案:

答案 0 :(得分:0)

请在adviceMethod.Hope的目标方法类中定义no args public或default类型构造函数,它将解决您的问题。

答案 1 :(得分:-1)

  

Could not instantiate bean class [...]: No default constructor found

这看起来更像是一个Spring bean配置/实例化问题,而不是与Spring AOP或AspectJ相关的任何问题。

修好后,您可能会遇到其他问题:

  • 在你的建议@Around(value = "@annotation(benchmark)")中,你应该注意拼写。我想你希望你的注释Benchmark以大写字母“B”开头,而不是小写字母。
  • 您的第二个建议参数final Benchmark benchmark来自哪里?它不受argsthistarget约束。无论如何,你不要在你的建议中使用它,所以你可以删除它。

更新以回答您的其他问题:

AopConfigException: Could not generate CGLIB subclass of class
[class com.test.AttributeContainer]: Common causes of this problem
include using a final class or a non-visible class;
nested exception is java.lang.IllegalArgumentException:
Superclass has no null constructors but no arguments were given

此异常消息似乎非常明显。你看过吗?您可以在没有参数的情况下实例化组件,但只提供绝对需要参数的构造函数。这就是Spring抱怨的原因。也许你想阅读Spring教程?