xxxxxx because it is a JDK dynamic proxy that implement

because it is a JDK dynamic proxy that implement 

2019-07-31 16:37:06.407 ERROR 30036 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter    : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'privilegeFilterRegistration' defined in class path resource [com/ewt360/dataapi/privilege/filter/FilterRegistrationConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method 'privilegeFilterRegistration' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'privilegeFilter': Unsatisfied dependency expressed through field 'apiBaseService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiBaseServiceImpl': Unsatisfied dependency expressed through field 'apiHelperService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'apiHelperService' is expected to be of type 'com.ewt360.dataapi.biz.common.ApiHelperService' but was actually of type 'com.sun.proxy.$Proxy143'
2019-07-31 16:37:06.443  WARN 30036 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
2019-07-31 16:37:06.464  INFO 30036 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-07-31 16:37:06.470 ERROR 30036 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'apiHelperService' could not be injected as a 'com.ewt360.dataapi.biz.common.ApiHelperService' because it is a JDK dynamic proxy that implements:


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

出现这种异常可能有两个原因:
1.使用spring-boot做事务管理时,出现异常:The bean 'xxx' could not be injected as a 'xx.xxxx' because it is a JDK dynamic proxy that implements:

搞了半天发现是因为代理的原因
解决方法:
① 使用 @Transactional
开启@Transactional 注解支持,两种方式,一种是通过xml的方式

  <annotation-driven transaction-manager="txManager">
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    </annotation-driven>

② 另外一种是使用@EnableTransactionManagement,将该注解标注在@Configuration类上,等价于上面的<tx:annotation-driven/>
Spring推荐奖该注解标记在类(方法)而不是接口,将注解标记在接口上时,只有使用动态代理的时候才会生效,需要标记proxy-target-class="true"或者mode="aspectj",如下:


 

@RestController
@SpringBootApplication
@EnableCaching
/**
 * 在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener
 * 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
 * */
@ServletComponentScan
@EnableScheduling
@EnableTransactionManagement(proxyTargetClass = true)
@EnableSwagger2
public class App {

}

  CGLib 方式实现AOP代理问题

  1. 当我们需要使用CGLIB来实现AOP的时候,需要配置spring.aop.proxy-target-class=true,不然默认使用的是标准Java的实现,就会报错。
    解决方法:在yml文件中加入
spring:

  aop:
    proxy-target-class: true

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值