在不是spring管理的对象、无法直接取得ApplicationContext的地方获取Bean,如web servlet、其他框架等。
环境:与第一、二章相同。依赖于之前创建的工程、库、接口定义、xml或注解配置。
参照RestEasy的两种实现方法:
1.通过WebApplicationContextUtils工具类获取ApplicationContext对象
获取到ApplicationContext就能获取Bean:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);
UserDao userDao = (UserDao)context.getBean("userDao");
struts2中可通过以下代码获取ServletContext:
ServletContext application = ServletActionContext.getRequest().getSession().getServletContext();
2.自定义工具类
定义Context工具类SpringContextHolder:
package com.sunbin.common.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBean(clazz);
}
/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException(
"applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
root-context.xml增加配置:
<!-- spring context holder -->
<bean class="com.sunbin.common.spring.SpringContextHolder"
lazy-init="false" />
工具类中有静态变量applicationContext和非静态set方法,spring加载配置并创建ApplicationContext时,会创建工具类的一个单例bean并将ApplicationContext自身注入bean。之后可以通过工具类的静态方法获取applicationContext并获取Bean。
可通过beanName或beanType获取。
测试类代码:
package com.sunbin.test.testSpring.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sunbin.common.spring.SpringContextHolder;
import com.sunbin.test.testSpring.service.TestService;
public class TestSpringContextHolder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "root-context.xml" });
TestService testService =(TestService) SpringContextHolder.getBean("testService");
System.out.println(testService.test("helloWorld"));
testService =(TestService) SpringContextHolder.getBean(TestService.class);
System.out.println(testService.test("helloWorld"));
}
}
运行结果如下:
- testServiceImpl.test:testDaoImpl.test:helloWorld
- testServiceImpl.test:testDaoImpl.test:helloWorld
说明获取bean成功。
分享到:
相关推荐
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习...
获取springbean对象
Spring获取Bean类
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class...测试示例中是spring boot 的部分代码,动态加载的内容为接口实现类,且初始化时加载本地的实现类,动态加载后改为非程序加载目录中的jar实现类。
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring把Bean放在这个容器中,普通的类在需要的时候,直接用getBean()方法取出
Spring Bean创建初始化流程
spring动态向容器中添加bean和删除指定bean,不需要重启应用
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
JavaEE中的spring的基础bean的书写,和使用
spring bean 属性总结
在线程中获取spring注入对象,如:数据库连接对象,DAO,Service ,资源文件等
创建SpringBean配置工具类(安全)如: <bean id=... scope="prototype"></bean>
NULL 博文链接:https://1151461406.iteye.com/blog/2389888
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
spring运行过程中动态注册bean,代码实例
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
对Spring如何获取Bean做一下代码明细