Java代码6种方法获取spring 容器的bean
2017-03-18 21:45
507 查看
一:
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
context.getBean(" ")方法;
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
二:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext context1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext context2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
context1.getBean(" ");
context2.getBean(" ");
说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
三:
抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
四:
继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext。
五:
实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。
以上5种方法需要在xml文件中配置。
六:
最后提供一种不依赖于servlet,不需要注入的方式。注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器。
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
context.getBean(" ")方法;
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
二:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext context1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext context2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
context1.getBean(" ");
context2.getBean(" ");
说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
三:
抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
四:
继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext。
五:
实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。
以上5种方法需要在xml文件中配置。
六:
最后提供一种不依赖于servlet,不需要注入的方式。注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器。
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean("");
相关文章推荐
- Java代码5种方法获取spring 容器的bean
- java普通类获取Spring中bean方法
- 获取Spring 容器中bean的方法
- 普通Java类获取spring 容器的bean的5种方法
- Java类获取spring 容器的bean
- 随web容器启动Java类,调用spring容器中的的bean及其方法
- Spring如何管理Java普通类(Java类获取Spring容器的bean)
- Spring在代码中获取bean的方法小结
- Struts 2在Action中获取Spring容器中所有Bean实例名称的方法
- Spring线程获取容器中bean的方法
- 代码获取spring管理的javabean
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
- Java类获取spring 容器的bean
- Java 代码获取spring管理的bean对象
- Java类获取Spring容器的bean
- java 从spring容器中获取注入的bean对象
- Java类获取spring 容器的bean
- 获取Spring的Bean持有容器对象的方法
- Spring容器中获取bean实例的方法