关键词
JavaEE JavaWeb eclipse XML AspectJ
形容
1.报错记录。摸索中。轻喷。
2.《Java EE框架整合开发入门到实战:Spring+Spring MVC+MyBatis(微课版)》4.4节“基于XML配置开发AspectJ”
报错
正告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDao' defined in class path resource [aspectj/xml/applicationContext.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#1': Cannot create inner bean '(inner bean)#57175e74' of type [org.springframework.aop.aspectj.AspectJAfterReturningAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#57175e74': Cannot create inner bean '(inner bean)#7bb58ca3' of type [org.springframework.aop.config.MethodLocatingFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7bb58ca3': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Unable to locate method [afterReturning] on bean [myAspect]
查看
拖到最左边查看报错
Unable to locate method [afterReturning] on bean [myAspect]
就是说bean实例myAspect中的办法afterReturning有问题。
查看MyAspect.java中的办法afterReturning。与applicationContext.xml对应地位进行比拟。
发现是MyAspect.java中的办法afterReturning拼写有问题(多写了个n)。与applicationContext.xml对应不上。
解决
将MyAspect.java中的办法afterReturnning改为afterReturning。与applicationContext.xml中的afterReturning对应上。
总结
1.查看eclipse报错局部的最下面的最左边
2.查看xml文件以及报错提醒的bean对应java文件。
(补充)我的项目
构造
package aspectj.xml;
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 定义指标对象,应用4.2.1节的实现类--> <bean id="testDao" class="dynamic.jdk.TestDaoImpl"/> <!-- 定义切面--> <bean id="myAspect" class="aspectj.xml.MyAspect"/> <!-- AOP配置 --> <aop:config> <!-- 配置切面 --> <aop:aspect ref="myAspect"> <!-- 配置切入点,告诉加强哪些办法 --> <aop:pointcut expression="execution(* dynamic.jdk.*.*(..))" id="myPointCut"/> <!-- 将告诉与切入点关联 --> <!-- 关联前置告诉 --> <aop:before method="before" pointcut-ref="myPointCut" /> <!-- 关联后置返回告诉,在指标办法胜利执行后执行 --> <aop:after-returning method="afterReturning" pointcut-ref="myPointCut"/> <!-- 关联盘绕告诉--> <aop:around method="around" pointcut-ref="myPointCut"/> <!-- 关联异样告诉,没有异样产生时将不会执行加强,throwing属性设置告诉的第二个参数名称--> <aop:after-throwing method="except" pointcut-ref="myPointCut" throwing="e"/> <!-- 关联后置(最终)告诉,不论指标办法是否胜利都要执行--> <aop:after method="after" pointcut-ref="myPointCut"/> </aop:aspect> </aop:config></beans>
MyAspect.java
package aspectj.xml;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;//切面类,在此类中编写各种类型的告诉public class MyAspect { //前置告诉,应用JoinPoint接口作为参数取得指标对象信息 public void before(JoinPoint jp){ System.out.println("前置告诉:模仿权限管制"); System.out.println(",指标类对象:"+jp.getTarget()+",被加强解决的办法:"+jp.getSignature().getName()); } //后置返回告诉 public void afterReturnning(JoinPoint jp){ System.out.println("后置返回告诉:"+"模仿删除临时文件"); System.out.println(",被加强解决的办法"+jp.getSignature().getName()); } /** * 盘绕告诉 * ProceedingJoinPoint是JoinPoint的子接口,代表能够执行的指标办法 * 返回值的类型必须是一个Object * 必须一个参数是ProceedingJoinPoint类型 * 必须throws Throwable * @param pjp * @return * @throws Throwable */ public Object around(ProceedingJoinPoint pjp) throws Throwable{ //开始 System.out.println("盘绕开始:执行指标办法前,模仿开始事务"); //执行以后指标办法 Object obj = pjp.proceed(); //完结 System.out.println("盘绕完结:执行指标办法后,模仿敞开事务"); return obj; } //异样告诉 public void except(Throwable e){ System.out.println("异样告诉:"+"程序执行异样"+e.getMessage()); } //后置(最终)告诉 public void after(){ System.out.println("最终告诉:模仿开释资源"); }}
XMLAspectJTest.java
package aspectj.xml;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import dynamic.jdk.TestDao;public class XMLAspectJTest { //在主办法中应用Spring容器获取代理对象,并执行指标办法 public static void main(String[] args) { ApplicationContext appCon = new ClassPathXmlApplicationContext("/aspectj/xml/applicationContext.xml"); //从容器中获取加强后的指标对象 TestDao testDaoAdvice = (TestDao)appCon.getBean("testDao"); //执行办法 testDaoAdvice.save(); /* 前置告诉:模仿权限管制 ,指标类对象:dynamic.jdk.TestDaoImpl@45b9a632,被加强解决的办法:save 盘绕开始:执行指标办法前,模仿开始事务 保留 最终告诉:模仿开释资源 盘绕完结:执行指标办法后,模仿敞开事务 后置返回告诉:模仿删除临时文件 ,被加强解决的办法save */ }}
package dynamic.jdk;
TestDao.java
package dynamic.jdk;public interface TestDao { public void save(); public void modify(); public void delete();}
TestDaoImpl.java
package dynamic.jdk;//该实现类作为指标类,在代理类中对实现类的办法进行加强解决public class TestDaoImpl implements TestDao { @Override public void save() { System.out.println("保留"); } @Override public void modify() { System.out.println("批改"); } @Override public void delete() { System.out.println("删除"); }}