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

查看:406
本文介绍了Spring 3.0 / AOP / Aspectj: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。*。*(*)) ,return =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连接连接)throws Throwable {

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

if(httpSession!= null&(Usuario)httpSession.getAttribute(usuario)!= null&&((String)(Usuario)httpSession.getAttribute ))。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 包)



有一种方法可以通过命令行 aop.xml ,但请注意有关类加载的潜在问题。您必须确保加载java类的类加载器具有 weaver附加 a>,所以如果你有不使用LTW的选项,只需编织JDK类文件并使用这些编织类,你会很好。否则你可能会有一个鸡蛋和鸡蛋的问题。


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

解决方案

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.

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天全站免登陆