spring中junit 提示Failed to load ApplicationContext

错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext

    2:Error creating bean with name 'userService' defined in class path resource [UserService.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

    3:Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

根本原因:由于web在加载spring的配置文件(applicationContext.xml)时,发生了错误,而错误是3(cglib2不可用),所以根本原因是缺少cglib的包。

 

applicationContext.xml中的代理配置如下

  <!-- 指定使用cglib -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    <!-- 通过AOP配置提供事务增强,让service包下所有bean的所有方法拥有事务 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod" expression=" execution(* com.luxl.service..*(..))" />
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config>

解决方法:在maven中添加cglib的包,如下:

  <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

转载于:https://www.cnblogs.com/ScorchingSun/p/3924012.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Failed to load ApplicationContext"是一个错误消息,表示在加载应用程序上下文时发生了错误。根据引用的内容,这个错误通常与使用JUnit进行测试时出现。根据引用的内容,解决这个问题的方法是查看主启动类报错的最后一行,可能会提供更多关于错误的信息。另外,根据引用的内容,还有一种解决方法是将与spring-test相关的依赖的"<scope>test</scope>"去掉。这些参考资料可能会帮助你进一步解决"Failed to load ApplicationContext"的错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Failed to load ApplicationContext](https://download.csdn.net/download/weixin_38667835/13759257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [关于Failed to load ApplicationContext报错的原因](https://blog.csdn.net/mingpeihuang/article/details/124004500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Spring junit 测试报错:Failed to load ApplicationContext](https://blog.csdn.net/weixin_63577740/article/details/127222105)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值