我们可以使用设置请求属性Model
or ModelAndView
Spring 中的对象。
我们可以用@SessionAttributes
将属性保留在会话范围内。
那么我怎样才能将属性放入application
Spring中的作用域,spring是否为此提供了任何注释?
基本上配置应用程序范围所需的只是使用ServletContext
,你可以在 Spring 中执行如下操作:
public class MyBean implements ServletContextAware {
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
javax.servlet.ServletContext
甚至可以注入到您的 bean 实现中,如下所示:
@Component
public class MyBean {
@Autowired
private ServletContext servletContext;
public void myMethod1() {
servletContext.setAttribute("attr_key","attr_value");
}
public void myMethod2() {
Object value = servletContext.getAttribute("attr_key");
...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)