在具有最终成员且无默认构造函数的类上使用 Spring AOP

问题描述 投票:0回答:2

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

spring aop aspectj spring-aop cglib
2个回答
0
投票

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


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

© www.soinside.com 2019 - 2024. All rights reserved.