SpringBoot AOP 出现 BeanNotOfRequiredTypeException异常怎么办?

1.我的项目采用 Spring boot 开发.写了一个关于Aop的测试代码,报了如下异常信息:

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'performanceImpl' is expected to be of type 'com.itguang.demo.aop.PerformanceImpl' but was actually of type 'com.sun.proxy.$Proxy62'

具体代码如下:

一个接口 Performance

public interface Performance {
    public void  perform();
}

实现类 :PerformanceImpl


/**
 * @author itguang
 * @create 2017-11-07 8:26
 **/

public class PerformanceImpl implements Performance{

    @Override
    public void perform() {
        System.out.println("表演单口相声...");
    }
}

切面类:Audience2

/**
 * 观看演出的切面
 *
 * @author itguang
 * @create 2017-11-06 16:49
 **/
@Aspect
public class Audience2 {

    @Pointcut("execution(* com.itguang.demo.aop.Performance.perform(..))")
    public void performance(){

    }



    /**
     * 手机静音
     */
    @Before("performance()")
    public void  silenceCellPhone(){
        System.out.println("Silence Cell Phone");
    }

    /**
     * 就做
     */
    @Before("performance()")
    public void takeSeats(){
        System.out.println("Taking seats");
    }


    /**
     * 鼓掌
     */
    @AfterReturning("performance()")
    public void applause(){
        System.out.println("pa pa pa");
    }

    /**
     * 表演失败(异常),退款
     */
    @AfterThrowing("performance()")
    public void demandRefund(){
        System.out.println("Demanding a refund");
    }
}

配置类: AOPConfig

/**
 * AOP config
 *
 * @author itguang
 * @create 2017-11-06 16:53
 **/
@Configuration
@EnableAspectJAutoProxy
public class AOPConfig {

    @Bean
    public Audience2 audience2(){
        return new Audience2();
    }

    @Bean(name = "performanceImpl")
    public Performance performanceImpl(){
        return new PerformanceImpl();
    }


}

上面就是所有代码, 测试类如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAopApplicationTests {

    @Autowired
    private PerformanceImpl performanceImpl;

    @Test
    public void contextLoads() {
        performanceImpl.perform();
    }

}

运行报错代码完成如下

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.itguang.demo.SpringAopApplicationTests': Unsatisfied dependency expressed through field 'performanceImpl'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'performanceImpl' is expected to be of type 'com.itguang.demo.aop.PerformanceImpl' but was actually of type 'com.sun.proxy.$Proxy62'

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'performanceImpl' is expected to be of type 'com.itguang.demo.aop.PerformanceImpl' but was actually of type 'com.sun.proxy.$Proxy62'
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1510)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1489)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 28 more

如果我不采用接口的代理形式,就可以通过测试,但是基于借口的动态代理救火报错,不知道基于java配置的Spring应该怎样解决?

交互式爱情
浏览 505回答 3
3回答

临摹微笑

最后测试代码要使用接口注入。 @Autowired private PerformanceImpl performanceImpl; 更改为 @Autowired private Performance performanceImpl;

30秒到达战场

@Pointcut("execution(* com.itguang.demo.aop.Performance.perform(..))") 改成 @Pointcut("execution(* com.itguang.demo.aop.Performance+.perform(..))")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java