众所周知,Spring框架将DI模式发挥到了极至,因此,系统里面用Spring管理的Bean相互之间的获取是非常方便的,只要使用者提供一个setter方法并在配置文件中配置该属性就可以。
但是,对于系统中非Spring框架管理的类,如果需要获取Spring管理的类,或者,程序中需要动态的根据Bean的id来获取Bean实例,不可能事先为该类提供所有需要的Bean属性的setter方法,在类似这样的情况下,获取Spring框架管理的类实例的方法有多种,现在简单总结如下:
方法一:在初始化时保存ApplicationContext对象
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId");
这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象
import org.springframework.web.context.support.WebApplicationContextUtils; ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);ac1.getBean("beanId"); ac2.getBean("beanId");
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
方法四:继承自抽象类WebApplicationObjectSupport 类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware 实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。
我自己是在web.xml中配置,然后用一个监听器调用一个类直接读取,在tomcat启动时执行
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/applicationContext.xml</param-value>
这个类提供了方便的功能,这样你就不必去记 ServletContext 中属性的名字。 它的 getWebApplicationContext() 方法在 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 这个键值不对应任何对象的时候将返回 null。不过,为了避免在应用中得到 NullPointerExceptions ,我们推荐你使用 getRequiredWebApplicationContext() 方法。这个方法在ApplicationContext 缺失的时候会抛出一个异常。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
方法三:继承自抽象类ApplicationObjectSupport
抽象类ApplicationObjectSupport提供getApplicationContext
()
方法,可以方便的获取到
ApplicationContext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport 类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware 实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。
我自己是在web.xml中配置,然后用一个监听器调用一个类直接读取,在tomcat启动时执行
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>com.wzw.listener.SpringListener</listener-class>
</listener>
// TODO Auto-generated method stub
SpringBeanFactory.clear();
}
}
<listener>
<listener-class>com.wzw.listener.SpringListener</listener-class>
</listener>
SpringListener package com.wzw.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import com.wzw.spring.common.SpringBeanFactory; public class SpringListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub String relativePath = sce.getServletContext().getInitParameter( "contextConfigLocation"); String realPath = sce.getServletContext().getRealPath(relativePath);
SpringBeanFactory.init(realPath);
}
public void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub
SpringBeanFactory.clear();
}
}
监听器调用的类
package com.wzw.spring.common;
import java.util.Locale;
import org.apache.struts.action.ActionMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class SpringBeanFactory {
private static ApplicationContext context;
/**
* 在应用程序启动时配置spring框架
*
* @param filePath
*/
public static void init(String filePath) {
if (context == null) {
context = new FileSystemXmlApplicationContext(filePath);
}
}
public static ApplicationContext getContext(){
return context;
}
/**
* 方法用于获取业务对象。
*
* @param beanName
* @return
*/
public static Object getBusinessOjbect(String beanName) {
return context.getBean(beanName);
}
/**
* 在应用程序关闭时,清空spring框架配置信息。
*/
public static void clear() {
if (context != null) {
context = null;
}
}
}
这样就可以取到application,只要配置正确,再通过application取bean那是轻而易举的事!
package com.wzw.spring.common;
import java.util.Locale;
import org.apache.struts.action.ActionMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class SpringBeanFactory {
private static ApplicationContext context;
/**
* 在应用程序启动时配置spring框架
*
* @param filePath
*/
public static void init(String filePath) {
if (context == null) {
context = new FileSystemXmlApplicationContext(filePath);
}
}
public static ApplicationContext getContext(){
return context;
}
/**
* 方法用于获取业务对象。
*
* @param beanName
* @return
*/
public static Object getBusinessOjbect(String beanName) {
return context.getBean(beanName);
}
/**
* 在应用程序关闭时,清空spring框架配置信息。
*/
public static void clear() {
if (context != null) {
context = null;
}
}
}
这样就可以取到application,只要配置正确,再通过application取bean那是轻而易举的事!
相关推荐
主要介绍了JSP 获取spring容器中bean的方法总结的相关资料,需要的朋友可以参考下
主要介绍了Spring 应用上下文获取 Bean,结合实例形式总结分析了Spring 应用上下文获取 Bean的实现方法与操作注意事项,需要的朋友可以参考下
1.11 在spring中获取代理对象代理的目标对象工具类 1.12 如何为spring代理类设置属性值 1.13 我对SpringDAO层支持的总结 1.14 我对SpringDAO层支持的总结 1.15 我对SpringDAO层支持的总结 1.16 我对Spring 容器管理...
c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
spring框架技术+第2天+xmind思维导图:生命周期,介绍simple project,打印出构造方法...bean作用域request session globalSession:web项目获取核心配置文件要配置两个地方:spring监听器、spring作用域范围的监听。
获取bean 容器类层次结构 BeanFactory 3、核心容器总结 容器相关 bean相关 依赖注入相关 4、注解开发 注解开发定义bean 纯注解开发 bean管理 依赖注入 第三方bean管理 注解开发总结 5、Spring整合MyBatis ...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能...附录B 在Spring中开发Web Service
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能...附录B 在Spring中开发Web Service
12.2.3使用RequestPostProcessor在Spring MVC测试中以用户身份运行 106 作为用户在Spring MVC测试中使用注释运行 108 12.2.4测试HTTP基本认证 109 12.3 SecurityMockMvcRequestBuilders 109 12.3.1测试基于表单的...
SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...
文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...
Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式,如图 1 所示。 组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下: ☆...
2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3...
最佳实践总结了本人多年的Java开发中的一些开发经验以及工具类和Spring框架的应用采用了项目Demo的方式把零散的内容联系在一起去展示其用法,可以直接拿来作为种子项目,用于快速建造中小型的spring-boot项目项目...
查询出列表,也就是返回list, 在我们这个例子中也就是 List<User> , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...