UnsatisfiedDependencyException after adding aspect in spring project

Viewed 242

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

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

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

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

This is class:

@Slf4j
@NoArgsConstructor
public class AuditCustomUpdateAspect {

    @Autowired
    JdbcTemplate jdbcTemplate;*


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

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

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

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

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 Answers

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

Related