SpringBoot获取ServletContext和webApplicationConnect几种方法
2017-12-15 15:50
871 查看
获取ServletContext 的几种方法:
通过HttpServletRequest request获取
通过自动注入获取,该方法可以在@Controller下使用,暂未在Service中测试
通过WebApplicationContext获取
在SpringBoot中,通过ContextLoader获取的方法
目前失效。
可以通过自动注入
或者通过实现ApplicationContextAware
实现该接口的setApplicationContext可以注入ApplicationContext 。
可以通过@Component注入到spring中,然后在启动服务时,setApplicationContext会注入参数
获取到ApplicationContext后,可以通过getBean的方法 获取到bean工厂中的bean。
Assert.hasText(name,”name为空”);这个是属于spring的一个断言类工具。
@Component该注解不能省略。
在Controller中使用时,就通过@Autowired注入即可。
方法中使用
通过HttpServletRequest request获取
ServletContext sc = request.getServletContext();
通过自动注入获取,该方法可以在@Controller下使用,暂未在Service中测试
@Autowired private ServletContext servletContext;
通过WebApplicationContext获取
ServletContext servletContext = webApplicationConnect.getServletContext();
在SpringBoot中,通过ContextLoader获取的方法
目前失效。
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
可以通过自动注入
@Autowired WebApplicationContext webApplicationConnect;
或者通过实现ApplicationContextAware
实现该接口的setApplicationContext可以注入ApplicationContext 。
可以通过@Component注入到spring中,然后在启动服务时,setApplicationContext会注入参数
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }
获取到ApplicationContext后,可以通过getBean的方法 获取到bean工厂中的bean。
@Component public class SpringBeanTool implements ApplicationContextAware { /** * 上下文对象实例 */ private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationCont 4000 ext; } /** * 获取applicationContext * @return */ public ApplicationContext getApplicationContext() { return applicationContext; } /** * 通过name获取 Bean. * @param name * @return */ public Object getBean(String name){ return getApplicationContext().getBean(name); } /** * 通过class获取Bean. * @param clazz * @param <T> * @return */ public <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } /** * 通过name,以及Clazz返回指定的Bean * @param name * @param clazz * @param <T> * @return */ public <T> T getBean(String name,Class<T> clazz){ Assert.hasText(name,"name为空"); return getApplicationContext().getBean(name, clazz); } }
Assert.hasText(name,”name为空”);这个是属于spring的一个断言类工具。
@Component该注解不能省略。
在Controller中使用时,就通过@Autowired注入即可。
@Autowired SpringBeanTool springBeanTool;
方法中使用
UserService userService1 = springBeanTool.getBean("userService", UserService.class);
相关文章推荐
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- Spring获取webapplicationcontext,applicationcontext几种方法
- 【spring框架】spring获取webapplicationcontext,applicationcontext几种方法详解--(转)
- spring 获取 WebApplicationContext的几种方法
- spring获取webapplicationcontext,applicationcontext几种方法详解(转载)
- springboot获取URL请求参数的几种方法
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法
- Spring-boot Unable to start EmbeddedWebApplicationContext 分析与解决方法
- 【spring框架】spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法详解
- spring获取webapplicationcontext,applicationcontext几种方法
- spring获取webapplicationcontext,applicationcontext几种方法详解