springmvc中获取request对象,加载biz(service)的方法
获取request对象:
首先配置web.xml文件-->
- <listener>
- <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class>
- </listener>
然后在程序中获取:
代码:
- HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
在Controller中获取biz很简单,注解配置都行,一般用注解,@Autowired就搞定了,可是有时在普通java类中想获取到biz很麻烦,所以可以用下面的方式加载。
在普通类中加载biz,用SpringApplicationContext.getBean(xx.class);
代码:
- IKeyCodeBiz keyCodeBiz = SpringApplicationContext.getBean(IKeyCodeBiz.class);
版本二
利用Spring随时随地获得Request和Session
一、准备工作:
在web.xml中添加
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
二、使用方法:
1、方法一:通过代码实现
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
2、方法二:通过注解实现:
@Autowired
private HttpServletRequest request;
Request作用域
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
针对每次HTTP请求,Spring容器会根据loginAction bean定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。
Session作用域
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,你可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。
global session作用域
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。
请注意,假如你在编写一个标准的基于Servlet的web应用,并且定义了一个或多个具有global session作用域的bean,系统会使用标准的HTTP Session作用域,并且不会引起任何错误
二、为什么需要额外的配置RequestContextFilter
也许会有一个疑问,已经通过ContextLoaderListener(或ContextLoaderServlet)将Web容器与Spring容器整合,为什么这里还要用额外的RequestContextListener以支持Bean的另外3个作用域,原因是ContextLoaderListener实现ServletContextListener监听器接口,而ServletContextListener只负责监听Web容器的启动和关闭的事件。RequestContextFilter实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,Web服务器接收的每次请求都会通知该监听器。通过配置RequestContextFilter,Spring容器与Web容器结合的更加密切。
三、作用域依赖问题
如果将Web相关作用域的Bean注入到singleton或prototype的Bean中,这种情况下,需要Spring AOP
<bean name="car" class="com.demo.Car" scope="request">
<aop:scoped-proxy/>
</bean>
<bean id="boss" class="com.demo.Boss" >
<properrty name="car" ref="car" />
</bean>
springmvc中获取request对象,加载biz(service)的方法的更多相关文章
- 在SpringMVC中获取request对象
1.注解法 @Autowired private HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...
- 在SpringMVC中获取request对象的几种方式
1.最简单的方式(注解法) @Autowired private HttpServletRequest request; 2.最麻烦的方法 a. 在web.xml中配置一个监听 <listene ...
- 如何在SpringMVC中获取request对象
1.注解法 @Autowired private HttpServletRequest request; <listener> <listener-class> org.spr ...
- springMVC中获取request和response对象的几种方式(RequestContextHolder)
springMVC中获取request和response对象的几种方式 1.最简单方式:参数 2.加入监听器,然后在代码里面获取 原文链接:https://blog.csdn.net/weixin_4 ...
- SpringMvc中获取Request
Controller中加参数 @Controller public class TestController { @RequestMapping("/test") public v ...
- 如何在spring中获取request对象
1.通过注解获取(很简单,推荐): public class Hello {@Autowired HttpServletRequest request; //这里可以获取到request} 2.在w ...
- Struts2的Action中获取request对象的几种方式?
通过ActionContext.getSession获取 通过ServletActionContext.getRequest()获取 通过SessionAware接口注入 通过ServletReque ...
- java在方法中获取request对象
在spring的普通类中: HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getReques ...
- springMVC中得到request对象,session对象
RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((S ...
随机推荐
- 安装与学习laravel
安装 composer cd /var/www/html curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/ ...
- Django学习系列7:使用模板解决“不测试常量”规则,使用模板重构
之前写的lists/tests.py中的单元测试,要查找特定的HTML字符串,但这不是测试HTML的高效方法. 单元测试规则之一“不测试常量”,编写断言检测HTML字符串中是否有制定的字符串序列,不是 ...
- 502 bad gateway nginx 的错误的解决方案
总结 一般是php问题居多,也需要调整相应的nginx参数,最后也可能是mysql假死 nginx问题 查看日志中的报错error.log一般设置路径/usr/local/nginx/logs/ngi ...
- WPF 多个选项卡TabControl 页面分离
此项目源码下载地址:https://github.com/lizhiqiang0204/TabControl-page-separation 每个页面的按键处理事件直接对应该页面下的cs文件 Main ...
- JVM垃圾回收之CMS收集器
从前文JVM垃圾回收几种常见算法和常见收集器我们知道,CMS是老年代垃圾收集器.CMS 收集器主要关注系统停顿时间.CMS 是 Concurrent Mark Sweep 的缩写,意为并发标记清除,从 ...
- CodeForces-707B(思维)
链接: https://vjudge.net/problem/CodeForces-707B 题意: Masha wants to open her own bakery and bake muffi ...
- 【leetcode】1210. Minimum Moves to Reach Target with Rotations
题目如下: In an n*n grid, there is a snake that spans 2 cells and starts moving from the top left corner ...
- Power-Aware GateSim Debug
For PAG debug, the following steps may be useful. 1. Get correct netlists from PD which contain powe ...
- sh_08_打印分隔线
sh_08_打印分隔线 def print_line(char, times): print(char * times) print_line("hi ", 40)
- 模型层ORM操作
一.ORM操作 1.关键性字段及参数 DateField 年月日 DateTimeField 年月日时分秒 auto_now: 每次操作改数据都会自动更新时间 auto_now_add: 新增数据的时 ...