获取Application中的spring容器】的更多相关文章

方式1: ApplicationContext ac = (ApplicationContext) invocation.getInvocationContext().getApplication().get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 方式2: ServletContext sc = ServletActionContext.getServletContext(); ApplicationCont…
在项目中经常遇见需要在Listener中或者工具中使用Spring容器中的bean实例,由于bean不能在stataic的类中使用. 介绍一种方式: public class SpringTool { public static Object getObjectFromApplication(HttpSession session,String beanName){ ServletContext servletContext= session.getServletContext(); //通过W…
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容器中,无论是Servlet,Filter,还是Listener都不是Spring容器管理的,因此我们都无法在这些类中直接使用Spring注解的方式来注入我们需要的对象,当然除了下面我们详细说的方法外,还有的比如说为了在Servlet中使用Spring容器的对象,那么可以参考如下两篇文章: <Serv…
另外补充下:在web Server容器中,无论是Servlet,Filter,还是Listener都不是Spring容器管理的,因此我们都无法在这些类中直接使用Spring注解的方式来注入我们需要的对象,当然除了下面我们详细说的方法外,还有的比如说为了在Servlet中使用Spring容器的对象,那么可以参考如下两篇文章: <Servlet自动注入Spring容器中的Bean解决方法> <在servlet中注入spring的bean,servlet容器和spring容器> 额外文章…
---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托管对象该怎么办呢,很自然的我们需要获得spring容器对象的引用ApplicationContext,我的想法是在服务启动后,想办法将ApplicationContext容器的应用保存到一个静态变量中,以后使用就简单了. 1)刚开始用的是spring+struts2,实力话spring用的是Cont…
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("be…
学习https://github.com/thinkgem/jeesite 今天在写JedisUtils的时候要注入JedisPool,而这个属性被设置为static,@Resource和@Autowired都不可以注入,因为spring不能为静态变量依赖注入.因此需要额外的方法获取spring管理的bean.本文即SpringContextHolder: package com.demo.common.utils; import org.apache.commons.lang3.Validat…
第一种: 通常用ApplicationContext来调用Spring配置文件中的一些Bean,所以首先创建Spring上下文容器. ApplicationContext ac = (ApplicationContext) invocation.getInvocationContext().getApplication().get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 以上代码中invocation是Stru…
@Bean(autowire = Autowire.BY_NAME,value = "kaptchaProducer") public Producer kaptchaProducer() { Properties kaptchaProperties = new Properties(); kaptchaProperties.put("kaptcha.border", "no"); kaptchaProperties.put("kapt…
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手new的对象,想直接使用spring提供的其他对象或者说有一些不需要交给spring管理,但是需要用到spring里的一些对象: 虽然通过 ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.x…