我试图拦截对getConnection()方法的任何调用以设置dbms indentifier.我已经实现了一个方面来实现它,但是我什么也没得到.任何想法?谢谢!
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包一样),默认情况下不进行编织.
有一种方法可以通过command line和aop.xml消除该限制,但是请注意与类加载有关的潜在问题.您必须确保加载Java类的类加载器具有weaver attached,因此,如果可以选择不使用LTW,只需编织JDK类文件并使用这些编织类,就可以了.否则,您可能会遇到“母鸡和鸡蛋”的问题.