UnsatisfiedDependencyException在添加了切面后的Spring项目中。

huangapple 未分类评论50阅读模式
英文:

UnsatisfiedDependencyException after adding aspect in spring project

问题

我有一个项目,其中使用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,上述切入点引用了它:

```xml
<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>

<bean id="txManagerAudit"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceAudit"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceAudit"/>
</bean>

我大致了解Spring中AOP的工作原理。bean dataSourceAudit 的类 HikariDataSource 实现了一些接口,并且默认应用了JDK代理。在上述代码片段中,我试图应用 proxy-target-class=true,但仍然失败。我注意到当我添加这个设置时,已实现的接口会稍微改变 - org.springframework.cglib.proxy.Factory 会出现,但错误内容仍然相同。也许我最终未能将此设置应用于 HikariDataSource bean,这就是为什么它不起作用的原因?

提前感谢您的任何提示。


<details>
<summary>英文:</summary>

I have project with Spring configuration in XML file. I added below aspect with pointcut.

&lt;!-- language: xml --&gt;

    &lt;aop:aspectj-autoproxy/&gt;
    
    &lt;aop:config proxy-target-class=&quot;true&quot;&gt;
            &lt;aop:aspect id=&quot;customAuditAspect&quot; ref=&quot;customAudit&quot;&gt;
                &lt;aop:pointcut id=&quot;customAuditPointcut&quot;
                              expression=&quot;@target(lombok.NoArgsConstructor)&quot;/&gt;
                &lt;aop:before pointcut-ref=&quot;customAuditPointcut&quot; method=&quot;customAuditUpdate&quot;/&gt;
            &lt;/aop:aspect&gt;
    &lt;/aop:config&gt;

And this is a bean, which abovementioned pointcut refers to:

```xml
&lt;bean id=&quot;customAudit&quot; class=&quot;com.socha.modules.inspektr.aspect.AuditCustomUpdateAspect&quot;/&gt;

This is class:

<!-- language: java -->

@Slf4j
@NoArgsConstructor
public class AuditCustomUpdateAspect {

    @Autowired
    JdbcTemplate jdbcTemplate;*


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

When i deploy Web app with this feature, it complains in following way:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
  Error creating bean with name &#39;dataSourceAudit&#39;
  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 &#39;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&#39;
  to required type &#39;com.zaxxer.hikari.HikariConfig&#39;;

  nested exception is java.lang.IllegalStateException:
    Cannot convert value of type &#39;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&#39;
    to required type &#39;com.zaxxer.hikari.HikariConfig&#39;:
    no matching editors or conversion strategy found

Below I am attaching this bean with all its dependent beans:

<!-- language: xml -->

&lt;bean id=&quot;inspektrTransactionTemplate&quot;
      class=&quot;org.springframework.transaction.support.TransactionTemplate&quot;
      p:transactionManager-ref=&quot;txManagerAudit&quot; p:isolationLevelName=&quot;ISOLATION_READ_COMMITTED&quot;
      p:propagationBehaviorName=&quot;PROPAGATION_REQUIRED&quot;/&gt;

&lt;bean id=&quot;auditHikariCPConfig&quot; class=&quot;com.zaxxer.hikari.HikariConfig&quot;&gt;
    &lt;property name=&quot;poolName&quot; value=&quot;auditHikariCP&quot;/&gt;
&lt;/bean&gt;

&lt;bean id=&quot;dataSourceAudit&quot; class=&quot;com.zaxxer.hikari.HikariDataSource&quot; destroy-method=&quot;close&quot;&gt;
    &lt;constructor-arg ref=&quot;auditHikariCPConfig&quot;/&gt;
&lt;/bean&gt;

<bean id="txManagerAudit"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceAudit"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceAudit"/>
</bean>

I understand more or less how is AOP in Spring working. Class HikariDataSource of bean dataSourceAudit implements some interfaces and by default it applies JDK proxying. In above snippet I am trying to apply proxy-target-class=true, but it still fails. I see when I add this setting, that implemented interfaces changes a bit - org.springframework.cglib.proxy.Factory appears, but content of error is still the same. Maybe I am eventually failing to apply this setting on HikariDataSource bean and that's why it is not working?

Thank you in advance for any hints

答案1

得分: 0

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

谢谢。

英文:

This particular problem was solved by narrowing scope of the classes to be adviced , as R.G and Kriegaex suggested. Errors stopped occuring

Thank you

huangapple
  • 本文由 发表于 2020年6月6日 00:21:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/62219900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定