Spring 3.0/AOP/Aspectj:autoproxy 拦截对 getConnection() 的任何调用 [英] Spring 3.0 / AOP / Aspectj:autoproxy intercept any call to getConnection()

查看:24
本文介绍了Spring 3.0/AOP/Aspectj:autoproxy 拦截对 getConnection() 的任何调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拦截对 getConnection() 方法的任何调用以设置 dbms 标识符.我已经实现了一个方面来获得它,但我什么也没得到.任何的想法?谢谢!

I'm trying to intercept any call to getConnection() method to setup the dbms indentifier . I've implemented an aspect to get it but I don't get anything. Any idea? Thanks!

import java.sql.CallableStatement;
import java.sql.Connection;

import javax.servlet.http.HttpSession;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import es.iberia.tryp.model.entities.Usuario;

@Component
@Aspect
public class ConnectionAspect {

    @AfterReturning(value = "execution(java.sql.Connection javax.sql.DataSource+.getConnection(..))", returning = "connection")
    //@AfterReturning(value = "execution(java.sql.Connection org.springframework.jdbc.datasource.*.*(*))", returning = "connection")
    //@AfterReturning(value = "execution(java.sql.Connection java.sql.Connection *(..))", returning = "connection")
    //@AfterReturning(value = "execution(java.sql.Connection org.springframework.jdbc.datasource.DriverManagerDataSource.*(..))", returning = "connection")

    public void prepare (Connection connection) throws Throwable {

        HttpSession httpSession = (HttpSession) RequestContextHolder.currentRequestAttributes().resolveReference(RequestAttributes.REFERENCE_SESSION);

        if (httpSession!=null && (Usuario)httpSession.getAttribute("usuario")!=null && ((String)((Usuario)httpSession.getAttribute("usuario")).getNomina())!=null) {
            String nomina = (String)((Usuario)httpSession.getAttribute("usuario")).getNomina();
            String prepSql = "{ call DBMS_SESSION.SET_IDENTIFIER('" + nomina +"') }";
            CallableStatement cs = connection.prepareCall(prepSql);                             
            cs.execute();
            cs.close();         
        }
    }
} 

推荐答案

是的,我有一个想法:实际上你的切入点匹配所需的调用,但它们在 java 包中(如javax 包)默认被排除在编织之外.

Yes, I have an idea: Actually your pointcuts match the desired calls, but they are in the java package which (like the javax package) is excluded from weaving by default.

有一种方法可以通过 命令行aop.xml,但请注意关于类加载的潜在问题.您必须确保加载 java 类的类加载器具有 weaver 附加,因此,如果您可以选择不使用 LTW,只需编织 JDK 类文件并使用那些编织类,就可以了.否则,您可能会遇到鸡与蛋"的问题.

There is a way to remove that restriction via command line and aop.xml, but please be aware of potential problems concerning classloading. You have to make sure the classloader loading the java classes has a weaver attached, so if you have the option not to use LTW, just weave the JDK class files and use those woven classes, and you will be fine. Otherwise you might have a "hen and egg" problem.

这篇关于Spring 3.0/AOP/Aspectj:autoproxy 拦截对 getConnection() 的任何调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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