测试aop时出现一个bug:
配置完spring配置文件、测试用例之后运行,报错:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [mycom.test.aop.ArithmeticCalculator] is defined: expected single bean but found 0:
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
at mycom.test.aop.MainTest.main(MainTest.java:15)
原因分析:
报错信息的字面意思是没有找到ArithmeticCalculator类,开始时我以为是自动扫包配错了(– <context:component-scan base-package="mycom"></context:component-scan>
–),后来发现不是,是切面类中类的定位配错了,类的定位写成了类的接口地址;
(– @Before("execution(public int mycom.test.aop.ArithmeticCalculatorImpl.*(int, int))")
–)