java - 在spring项目中添加aspect后出现UnsatisfiedDependencyException

标签 java spring aop autowired aspect

我的项目在 XML 文件中包含 Spring 配置。我用切入点添加了以下方面。

<aop:aspectj-autoproxy/>

<aop:config proxy-target-class="true">
        <aop:aspect id="customAuditAspect" ref="customAudit">
            <aop:pointcut id="customAuditPointcut"
                          expression="@target(lombok.NoArgsConstructor)"/>
            <aop:before pointcut-ref="customAuditPointcut" method="customAuditUpdate"/>
        </aop:aspect>
</aop:config>

这是一个 bean,上面提到的切入点指的是:

<bean id="customAudit" class="com.socha.modules.inspektr.aspect.AuditCustomUpdateAspect"/>

这是类(class):

@Slf4j
@NoArgsConstructor
public class AuditCustomUpdateAspect {

    @Autowired
    JdbcTemplate jdbcTemplate;*


  public void customAuditUpdate() {
    log.warn("here I am");
  }
}

当我部署具有此功能的 Web 应用程序时,它会以以下方式提示:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
  Error creating bean with name 'dataSourceAudit'
  defined in ServletContext resource [/WEB-INF/spring-context/portlet-application-context.xml]:
  Unsatisfied dependency expressed through constructor parameter 0:
  Could not convert argument value of type [com.sun.proxy.$Proxy1719]
  to required type [com.zaxxer.hikari.HikariConfig]:
  Failed to convert value of type 'com.sun.proxy.$Proxy1719
  implementing org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.cglib.proxy.Factory,com.zaxxer.hikari.HikariConfigMXBean,org.springframework.core.DecoratingProxy'
  to required type 'com.zaxxer.hikari.HikariConfig';

  nested exception is java.lang.IllegalStateException:
    Cannot convert value of type 'com.sun.proxy.$Proxy1719
    implementing org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.cglib.proxy.Factory,com.zaxxer.hikari.HikariConfigMXBean,org.springframework.core.DecoratingProxy'
    to required type 'com.zaxxer.hikari.HikariConfig':
    no matching editors or conversion strategy found

下面我将此 bean 及其所有依赖 bean 附加到一起:

<bean id="inspektrTransactionTemplate"
      class="org.springframework.transaction.support.TransactionTemplate"
      p:transactionManager-ref="txManagerAudit" p:isolationLevelName="ISOLATION_READ_COMMITTED"
      p:propagationBehaviorName="PROPAGATION_REQUIRED"/>

<bean id="auditHikariCPConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="auditHikariCP"/>
</bean>

<bean id="dataSourceAudit" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="auditHikariCPConfig"/>
</bean>

我或多或少了解 Spring 中的 AOP 是如何工作的。 bean dataSourceAudit 的类 HikariDataSource 实现了一些接口(interface),默认情况下它应用 JDK 代理。在上面的代码片段中,我尝试应用 proxy-target-class=true,但它仍然失败。我看到当我添加此设置时,实现的接口(interface)发生了一些变化 - 出现 org.springframework.cglib.proxy.Factory ,但错误内容仍然相同。也许我最终未能在 HikariDataSource bean 上应用此设置,这就是它不起作用的原因?

提前感谢您的任何提示

最佳答案

正如 R.G 和 Kriega 所建议的那样,这个特殊问题是通过缩小要建议的类别范围来解决的。错误不再发生

谢谢

关于java - 在spring项目中添加aspect后出现UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62219900/

相关文章:

java - 如何在Spring Boot中的@AfterThrowing方法中访问对象

c# - 如何获取所有局部变量的转储?

java - 我的 Android 应用程序的 gameLoop 线程在退出时崩溃

java - 在Java中将马沿x轴和y轴旋转10度角

java - 使用 Spring Security 取消 Spring 流的安全

java - 有没有办法在 @Controller 方法中访问 HttpRequest 类型

Spring AOP : What's the difference between JoinPoint and PointCut?

Java Spring 3 MVC Controller 显式加载服务

java - SocketTimeoutException : Read timed out, 如何解决?

spring - 如何从Spring Data REST产生的表示中删除超媒体元素?