关键词

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.xsdhttp://www.springframework.org/schema/aophttp://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 {@Overridepublic void save() {System.out.println("保存");}@Overridepublic void modify() {System.out.println("修改");}@Overridepublic void delete() {System.out.println("删除");}
}

运行结果

(报错解决)Exception encountered during context initialization相关推荐

  1. 使用swagger报错:Exception encountered during context initialization - cancelling refresh attempt解决方法

    今天在使用swagger时遇到了如下报错: WARN 19704 --- [ main] ConfigServletWebServerApplicationContext :Exception enc ...

  2. Spring报错:Exception encountered during context initialization - cancelling refresh attempt: org.sprin

    在使用aop时报错: 十一月 06, 2021 4:15:21 下午 org.springframework.context.support.AbstractApplicationContext re ...

  3. Spring AOP 报错 Exception encountered during context initialization - cancelling refresh attempt[已解决]

    三月 19, 2018 4:01:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh ...

  4. Exception encountered during context initialization(报错详解)

    Exception encountered during context initialization报错详细解决方案 基于XML配置的学习过程中运行出现错误 前言 自己在学习依赖注入时,配置xml文 ...

  5. Exception encountered during context initialization - cancelling refresh attempt--依赖缺失

    项目启动报错: 报错内容: Exception encountered during context initialization - cancelling refresh attempt: org. ...

  6. Exception encountered during context initialization - cancelling refresh attempt

    场景 在main/java包下创建了两个类Student和Address ,其中Student类中定义了一个Address类的变量,private Address address:此时在resourc ...

  7. JavaSpring中Exception encountered during context initialization - cancelling refresh attempt:

    分享我的一个问题解决: 在module-info-java中添加相关exports即可 module test {requires spring.context;requires junit;expo ...

  8. 关于异常:警告: Exception encountered during context initialization - cancelling refresh attempt

    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...

  9. 其中之一原因Exception encountered during context initialization - cancelling refresh attempt: org

    在项目注入资源依赖的时候报错,刚开始的时候以为是数据库连接的问题,然后重建项目连入数据库,排除数据库连接:然后@注解和scan扫描路径查看,都没有问题,但是配置内容查看也没有问题(上次配置文件留下了坑 ...

  10. 警告: Exception encountered during context initialization - cancelling refresh attempt:

    今天写Spring遇到了一个坑爹的问题,那么因为啥原因呢? 错误提示我错误的加载了Bean 警告: Exception encountered during context initializatio ...

最新文章

  1. Android之二维码生成与扫描
  2. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
  3. django16: csrf跨站请求伪造/CSRF相关装饰器
  4. yum配合rpm查看软件包安装位置
  5. SASS用法指南(转)
  6. 企业信息化BI项目的整体思想和架构
  7. python 知乎 合并 pdf_如何用Python程序将几十个PDF文件合并成一个PDF?其实只要这四步...
  8. 本文介绍在Wireshark网络协议分析仪中如果解密SSL和TLS流量
  9. 如何在eclipse配置服务器server (java ee)
  10. ##免费的标准股票交易接口封装与实盘使用
  11. 读取xml文件转成ListT对象的两种方法
  12. UML--核心元素之参与者Actor
  13. vue3的自定义指令directives
  14. Vim配置及使用技巧
  15. AAL模版 中英文对照
  16. 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
  17. 利用solidworks钣金的通风口命令设计风扇罩,solidworks风扇罩如何设计
  18. COCOS2d_js三消项目基本功能实现
  19. Citrix NetScaler VPX ---基础1
  20. 如何读论文?复现代码?

热门文章

  1. 关于java.security.AccessControlException: access denied 的解决方法
  2. 华为PPPoE、PPP/MP、IP-Trunk配置
  3. Firefox的下载经管器:FlashGot v1.0 Final发布
  4. springboot:运行(部署)时出现WebServerException: Unable to create tempDir.
  5. 导航标签html,导航标签
  6. android界面设计开发总结
  7. MPEG音频文件格式(包括MP3文件格式)详解
  8. 使用CSS3制作水晶按钮
  9. sql做题记录(一)
  10. C# LeetCode刷题 - LeetCode 148. Sort List 解题报告(归并排序小结)