Spring 3.0/AOP/AAspectj:autoproxy拦截对getConnection()的任何调用



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

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();         
        }
    }
} 

检查是否在xml文件中添加了以下标记。

aop:aspectj自动代理

还要检查您是否已经在这个ConnectionAspect类的xml中添加了bean定义。

是的,我有一个想法:实际上,您的切入点与所需的调用相匹配,但它们在java包中,该包(如javax程序包)默认情况下被排除在编织之外。

有一种方法可以通过命令行和aop.xml来消除这种限制,但请注意与类加载有关的潜在问题。你必须确保加载java类的类加载器附带了一个weaver,所以如果你可以选择不使用LTW,只需编织JDK类文件并使用这些编织的类,你就可以了。否则,你可能会遇到"母鸡和鸡蛋"的问题。

最新更新