问题
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'accountService\' defined in file [D:\\IdeaProjects\\spring-study\\day03-proxy-aop\\target\\classes\\com\\wei\\service\\impl\\AccountServiceImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.AfterThrowing(throwing=, pointcut=, value=, argNames=) is not a valid AspectJ annotationException in thread \"main\" org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'accountService\' defined in file [D:\\IdeaProjects\\spring-study\\day03-proxy-aop\\target\\classes\\com\\wei\\service\\impl\\AccountServiceImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.AfterThrowing(throwing=, pointcut=, value=, argNames=) is not a valid AspectJ annotationat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:512)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)at AOPTest.main(AOPTest.java:8)Caused by: java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.AfterThrowing(throwing=, pointcut=, value=, argNames=) is not a valid AspectJ annotationat org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory$AspectJAnnotation.<init>(AbstractAspectJAdvisorFactory.java:201)at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.findAnnotation(AbstractAspectJAdvisorFactory.java:146)at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(AbstractAspectJAdvisorFactory.java:134)at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.lambda$static$0(ReflectiveAspectJAdvisorFactory.java:85)at org.springframework.core.convert.converter.ConvertingComparator.compare(ConvertingComparator.java:79)at java.util.Comparator.lambda$thenComparing$36697e65$1(Comparator.java:216)at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)at java.util.TimSort.sort(TimSort.java:220)at java.util.Arrays.sort(Arrays.java:1512)at java.util.ArrayList.sort(ArrayList.java:1462)at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisorMethods(ReflectiveAspectJAdvisorFactory.java:171)at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisors(ReflectiveAspectJAdvisorFactory.java:130)at org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder.buildAspectJAdvisors(BeanFactoryAspectJAdvisorsBuilder.java:110)at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:95)at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:101)at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:251)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1141)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1114)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:506)... 10 moreCaused by: java.lang.IllegalStateException: Failed to resolve expression: @org.aspectj.lang.annotation.AfterThrowing(throwing=, pointcut=, value=, argNames=)at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory$AspectJAnnotation.resolveExpression(AbstractAspectJAdvisorFactory.java:223)at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory$AspectJAnnotation.<init>(AbstractAspectJAdvisorFactory.java:196)... 28 more
原因
这两个注解虽然不会爆红,但是没有指定execution还是会在运行时出现异常。
解决
@Before(\"pt1()\")public void beforePrintLog(){System.out.println(\"前置。Logger类的printLog方法开始记录日志了。beforePrintLog\");}@AfterReturning(\"pt1()\")public void afterReturningPrintLog(){System.out.println(\"后置。Logger类的printLog方法开始记录日志了。afterReturningPrintLog\");}@AfterThrowing(\"pt1()\")public void afterThrowingPrintLog(){System.out.println(\"异常。Logger类的printLog方法开始记录日志了。afterThrowingPrintLog\");}@After(\"pt1()\")public void afterPrintLog(){System.out.println(\"最终。Logger类的printLog方法开始记录日志了。afterPrintLog\");}