• 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>

  • 相关阅读:
    idea spring boot 1.x junit单元测试
    linux oracle/jdk启用大页面
    jdk8之CompletableFuture与CompletionService
    gc日志深入解析-覆盖CMS、并行GC、G1、ZGC、openj9
    h2 web console使用
    LockSupport工具类详解
    反射、Unsafe、直接调用性能大比拼
    spring boot druid动态多数据源监控集成
    Linux网络
    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection总结
  • 原文地址:https://www.cnblogs.com/ScorchingSun/p/3924012.html
Copyright © 2020-2023  润新知