有关spring的aop中 AfterAdvice.java (不传参数)获取servlet中application,或request的方法代码如下,;
<bean name="testAutoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>userTest</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>afterAdvice</value>
</list>
</property>
</bean> <bean id="afterAdvice" class="xx.xxx.AfterAdvice">
</bean>
java :public class AfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
if("true".equals(returnValue))//如果返回值为真
{ mytest对象 = args[0];
//application.setAttribute(“test”,mytest);//代码想这样写,但是我不知道怎么去获取application
//mytest对象 =application.getAttribute(“test”);
} }高分求高手帮我解决下,
<bean name="testAutoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>userTest</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>afterAdvice</value>
</list>
</property>
</bean> <bean id="afterAdvice" class="xx.xxx.AfterAdvice">
</bean>
java :public class AfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
if("true".equals(returnValue))//如果返回值为真
{ mytest对象 = args[0];
//application.setAttribute(“test”,mytest);//代码想这样写,但是我不知道怎么去获取application
//mytest对象 =application.getAttribute(“test”);
} }高分求高手帮我解决下,
获取 application 对象
ServletActionContext.getContext().getApplication();
或者ServletContext.getContext().getApplication();
其中ServletActionContext类的静态方法getContext()来获取该对象
ActionContex是它的父类
就可以了
看来你没有使用Struts2
<bean id="afterAdvice" class="xx.xxx.AfterAdvice">
</bean>
在你的配置文件中加入你要使用ServletContext的对象Bean,或者不一定注入ServletContext,只要继承于他就可以然后在你的类中就能使用,获取Servlet相关对象了
public class AfterAdvice implements AfterReturningAdvice,ServletContextAware {
public void setServletContext(ServletContext arg0) {
this.servletContext = arg0;
}
private ServletContext servletContext;
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
if("true".equals(returnValue))//如果返回值为真
{
servletContext.setAttribute("myServletContext", "servletContext here ................");
}
}