1

我正在尝试使用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 无法创建子类?谢谢!

4

2 回答 2

0

请在adviceMethod.Hope的目标方法类中定义一个无参数公共或默认类型构造函数,它将解决您的问题。

于 2016-01-25T11:26:27.097 回答
-1

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

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

解决此问题后,您可能会遇到其他问题:

  • 在您的建议@Around(value = "@annotation(benchmark)")中,您应该注意拼写。我猜您希望您的注释Benchmark以大写“B”开头,而不是小写字母。
  • 您的第二个建议参数final Benchmark benchmark来自哪里?它不通过args,this或绑定target。无论如何,您都不会在建议中使用它,因此您可以将其删除。

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

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 教程?

于 2013-05-05T13:11:23.180 回答