Spring工具类:WebApplicationContextUtils

简介:

WebApplicationContextUtils 可以获取

1
2
WebApplicationContext
WebApplicationContext 可以获取bean,然后执行方法获取数据。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package  cn.sccl.common.web;
 
import  java.util.List;
import  java.util.Map;
 
import  javax.servlet.ServletContext;
import  javax.servlet.ServletContextEvent;
import  javax.servlet.ServletContextListener;
 
import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.springframework.context.ApplicationContext;
import  org.springframework.web.context.ContextLoaderListener;
import  org.springframework.web.context.support.WebApplicationContextUtils;
 
import  cn.sccl.common.service.BizCodeManager;
import  cn.sccl.common.web.util.Log4jWebConfigurer;
import  cn.sccl.pms.model.Division;
import  cn.sccl.pms.model.DivisionQuery;
import  cn.sccl.pms.service.DivisionManager;
 
public  class  StartupListener  extends  ContextLoaderListener  implements  ServletContextListener {
 
     @Override
     public  void  contextInitialized(ServletContextEvent event) {
         super .contextInitialized(event);
         ServletContext context = event.getServletContext(); //获取servletContext
         //也可以在实现了HttpServlet接口中获取,ServletContext servletContext = this.getServletContext();  
         setupContext(context);
     }
 
     protected  void  setupContext( final  ServletContext context) {
//       WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
          //推荐使用这种,因为getRequiredWebApplicationContext要求servletContext中必须要有ApplicationContext
         ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
      
         // 行政区划   
         DivisionManager divisionManager = (DivisionManager) ctx.getBean( "divisionManager" ); //得到manager
         List<Division> divisions = divisionManager.query( new  DivisionQuery());
         //将数据放入ServletContext 页面中就在application中获取,因为application和servletContext对应
         context.setAttribute( "divisions" , divisions);
     }
 
 
}





      本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/1890712,如需转载请自行联系原作者





相关文章
|
Java Spring
解决Spring工具类BeanUtils copyProperties方法复制null的问题
解决Spring工具类BeanUtils copyProperties方法复制null的问题
1165 0
|
11月前
|
Java 测试技术 Spring
springboot学习三:Spring Boot 配置文件语法、静态工具类读取配置文件、静态工具类读取配置文件
这篇文章介绍了Spring Boot中配置文件的语法、如何读取配置文件以及如何通过静态工具类读取配置文件。
880 0
springboot学习三:Spring Boot 配置文件语法、静态工具类读取配置文件、静态工具类读取配置文件
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
520 3
|
开发框架 Java 数据库
|
缓存 小程序 Java
别再造反射轮子了,Spring中ReflectionUtils 工具类,应有尽有!
ReflectionUtils是spring针对反射提供的工具类。
|
存储 NoSQL Java
Spring Boot 如何编写一个通用的 Redis 工具类
Spring Boot 如何编写一个通用的 Redis 工具类
698 0
Spring Boot 如何编写一个通用的 Redis 工具类
|
Java Apache Spring
Spring BeanUtils 2、Cglib BeanCopier 3、Apache BeanUtils 4、Apache PropertyUtils 5、Dozer 那么,我们到底应该选择哪种工具类更加合适呢?为什么Java开发手册中提到禁止使用Apache BeanUtils呢
Spring BeanUtils 2、Cglib BeanCopier 3、Apache BeanUtils 4、Apache PropertyUtils 5、Dozer 那么,我们到底应该选择哪种工具类更加合适呢?为什么Java开发手册中提到禁止使用Apache BeanUtils呢
171 0
|
Java Spring
【Spring】org.springframework.util.StringUtils工具类中commaDelimitedListToStringArray的使用
【Spring】org.springframework.util.StringUtils工具类中commaDelimitedListToStringArray的使用
156 0
|
Java Spring
动态获取spring管理的bean工具类
动态获取spring管理的bean工具类
289 0
|
Java 开发者 Spring
Spring断言工具类 “Assert” 的基本操作!
Spring断言工具类 “Assert” 的基本操作!
409 0
Spring断言工具类 “Assert” 的基本操作!