有关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”);
} }高分求高手帮我解决下,

解决方案 »

  1.   

    引用://application.setAttribute(“test”,mytest);//代码想这样写,但是我不知道怎么去获取application 
    获取 application 对象
    ServletActionContext.getContext().getApplication();
    或者ServletContext.getContext().getApplication();
      

  2.   

    我是AfterAdvice.java中没有ServletActionContex 对象
      

  3.   

    ServletActionContex 对象是Struts2里面的一个类
    其中ServletActionContext类的静态方法getContext()来获取该对象
    ActionContex是它的父类
      

  4.   

    HttpServletRequest request = ServletActionContext.getRequest();
    就可以了
    看来你没有使用Struts2
      

  5.   

    使用注入,Spring就干这个,
    <bean id="afterAdvice" class="xx.xxx.AfterAdvice"> 
    </bean> 
    在你的配置文件中加入你要使用ServletContext的对象Bean,或者不一定注入ServletContext,只要继承于他就可以然后在你的类中就能使用,获取Servlet相关对象了
      

  6.   

    应该可以实现spring的ServletContextAwareimport org.springframework.web.context.ServletContextAware;
    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 ................");
                                  

    }