Spring方面没有在getConnection上被解雇 [英] spring aspect not getting fired on getConnection

查看:122
本文介绍了Spring方面没有在getConnection上被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在春季3.2.3中拦截getConnection调用

I am trying to intercept the getConnection call in spring 3.2.3

@Component
@Aspect
@Order(value = 1)
public class ConnectionAspect {

    //@AfterReturning(pointcut = "execution(java.sql.Connection javax.sql.DataSource.getConnection(..))", returning = "connection")
    @Around("execution(java.sql.Connection javax.sql.DataSource.getConnection(..))")
    public Connection prepare(ProceedingJoinPoint pjp) throws Throwable {
        return MyConnectionProxy.newInstance((Connection) pjp.proceed(pjp.getArgs()));
    }

}

在调用getConnection时不会调用此方面.切入点定义 execution(java.sql.Connection javax.sql.DataSource.getConnection(..))

This aspect does not invoked on calling getConnection . Is there any mistake in the point cut definition execution(java.sql.Connection javax.sql.DataSource.getConnection(..))

推荐答案

Spring AOP只能建议Spring托管bean.如果您的 DataSource 实例不是Spring托管的bean,那么您将无法使用Spring AOP实现您的目标.

Spring AOP can only advise spring managed beans. If your DataSource instances are not spring managed beans, you won't be able to achieve your goal with Spring AOP.

我将尝试通过在提供的DataSource容器周围创建某种委托代理来解决此问题,并使其成为Spring管理的bean.事实证明,实际上在Spring中有一个专门用于此目的的类.它称为 <代码> DelegatingDataSource .您只需要继承该类,即可遍历 getConnection()方法(或您需要影响的其他任何方法的行为),并将其设置为委派给提供的 DataSource 容器,并使其成为Spring托管的bean,您就可以开始了.

I would try to solve this issue by creating some kind of delegating proxy around the container provided DataSource, and make it a bean managed by spring. It turns out there's actually a class intended in Spring specifically for this purpose. It's called DelegatingDataSource. You only need to subclass this class, ovverride the getConnection() method (or whichever other method's behavior you need to affect), set it up for delegating to the container provided DataSource, and making it a spring managed bean and you're good to go.

可以通过以下示例进行操作:

Someting along this example should do it:

@Configuration
public class DataSourceConfiguration {

    public static class MySpecialDataSource extends DelegatingDataSource {

        public MySpecialDataSource(DataSource delegate) {
            super(delegate);
        }

        @Override
        public Connection getConnection() throws SQLException {
            return super.getConnection();
        }
    }

    @Bean
    public DataSource dataSource(@Autowired DataSource containerDataSource) {
        return new MySpecialDataSource(containerDataSource);
    }

    @Bean(name="containerDataSource")
    public JndiObjectFactoryBean containerDataSource() {
        JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
        factoryBean.setJndiName("jdbc/MyDataSource");
        return factoryBean;
    }

}

最好的事情是您甚至不需要Spring AOP或AspectJ.

The best thing is that you didn't even need Spring AOP or AspectJ for that.

这篇关于Spring方面没有在getConnection上被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆