2

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

kriegaex
  • 63,017
  • 15
  • 111
  • 202
BartekS
  • 127
  • 1
  • 1
  • 7
  • This [answer](https://stackoverflow.com/a/53452483/4214241) from kriegaex explains the probable reason for `@target` failing in your case . Could you please try limiting the scope of the classes to be adviced ? eg : `@target(lombok.NoArgsConstructor) and within(com.socha.modules.inspektr..*)` – R.G Jun 06 '20 at 02:52
  • This looks interesting. Do you have something I can clone, compile and run for me, a full [MCVE](https://stackoverflow.com/help/mcve)? I see no obvious reason for your problem, but I was just parsing your code and config in my head. For now I also do not think that my other answer which @R.G linked to is related to your problem. The only funny thing I see is that you are targeting `@NoArgsConstructor` and the aspect itself has the same annotation, but actually in Spring AOP aspects are exempt from being pointcut targets themselves (only in AspectJ you can intercept aspect A from aspect B). – kriegaex Jun 07 '20 at 10:44

1 Answers1

0

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

BartekS
  • 127
  • 1
  • 1
  • 7