【发布时间】:2020-06-05 16:21:25
【问题描述】:
我在 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 dataSourceAudit 的类 HikariDataSource 实现了一些接口,默认情况下它应用 JDK 代理。在上面的 sn-p 中,我试图申请 proxy-target-class=true,但它仍然失败。我看到当我添加这个设置时,实现的接口发生了一些变化 - 出现org.springframework.cglib.proxy.Factory,但错误内容仍然相同。也许我最终无法在 HikariDataSource bean 上应用此设置,这就是它不起作用的原因?
提前感谢您的任何提示
【问题讨论】:
-
来自 kriegaex 的 answer 解释了
@target在您的情况下失败的可能原因。您能否尝试限制要建议的课程的范围?例如:@target(lombok.NoArgsConstructor) and within(com.socha.modules.inspektr..*) -
这看起来很有趣。你有什么我可以为我克隆、编译和运行的东西吗,一个完整的MCVE?我认为您的问题没有明显的原因,但我只是在脑海中解析您的代码和配置。目前,我也不认为@R.G 链接到的其他答案与您的问题有关。我看到的唯一有趣的事情是,您的目标是
@NoArgsConstructor,并且切面本身具有相同的注释,但实际上在 Spring AOP 中切面本身不受切入点的影响(仅在 AspectJ 中,您可以从切面 B 中截取切面 A)。
标签: java spring aop autowired aspect