在Spring项目中添加方面之后,UnsatisfiedDependencyException

问题描述 投票:0回答:1

我在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"/>

这是课程:

@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 HikariDataSource的类dataSourceAudit实现了一些接口,并且默认情况下它应用JDK代理。在以上代码段中,我尝试应用proxy-target-class=true,但仍然失败。我看到当我添加此设置时,已实现的接口会有所变化-出现org.springframework.cglib.proxy.Factory,但错误内容仍然相同。也许我最终无法在HikariDataSource bean上应用此设置,这就是为什么它不起作用的原因?

预先感谢您的任何提示

java spring aop autowired aspect
1个回答
0
投票

如R.G和Kriegaex所建议,通过缩小要建议的类的范围来解决此特定问题。错误停止发生

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.