javaWeb普通类获取ApplicationContext】的更多相关文章

网上看了很多关于获取ApplicationContext的方法,五大方法,但是我用web服务使用成功的就这一个,自己记忆下. import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context…
转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContext方式 我自己常用的方法: 读取一个文件1 //创建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); //获取chinese 实例 Person p = ctx.getBean…
方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSupport方法四:继承自抽象类WebApplicationObjectSupport方法五:实现接口ApplicationContextAware 常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext…
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId");说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供…
方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供的工具类获取ApplicationConte…
在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationContextAware接口去获得Spring上下文.创建以下的类: package com.school.tool; import org.springframework.beans.BeansException; import org.springframework.context.Applicat…
转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring/applicationContext.xml" }) public class SpringTest {} 想要获取Applicat…
工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取spring容器对象,从而获取容器中的bean. 直接上代码,如下: package com.blue.forum.context; import org.springframework.beans.BeansException; import org.springframework.beans.fac…
1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;…
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行需要加载的类的配置:不能在spring-mvc.xml中配置: beans.xml中的配置: <bean id="deployMode" class="java.lang.String"> <constructor-arg> <value&…