BeanFactory为一个管理bean的工厂(即为spring的容器),它管理的对象可以是bean也可以是FactoryBean(这种请况会再调用FactoryBean的getObject()获取真正的bean)。
FactoryBean为一个工厂bean,受BeanFactory管理。
先来看一看BeanFactory:
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String name);
Object getBean(String name, Class requiredType);
boolean containsBean(String name);
boolean isSingleton(String name);
boolean isPrototype(String name);
boolean isTypeMatch(String name, Class targetType);
Class getType(String name);
String[] getAliases(String name);
}
|
BeanFactory调用者只需要调用getBean方法即可获得指定bean的引用。
ApplicationContext继承自BeanFactory,它拥有BeanFactory提供的所有配置框架和基本的功能, 而且ApplicationContext为它增加了更强的功能。因此通常在J2EE环境的应用中,最好选择使用ApplicationContext下的子类。
Web应用通过在web.xml中的配置
加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet。
这两者在功能上完全等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。开发中可根据目标Web容器的实际情况进行选择。中,
配置非常简单,在web.xml中增加:
这两者在功能上完全等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。开发中可根据目标Web容器的实际情况进行选择。中,
配置非常简单,在web.xml中增加:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
|
或:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
|
通过以上配置,Web容器会自动加载/WEB-INF/applicationContext.xml初始化
ApplicationContext实例,如果需要指定配置文件位置,可通过context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
|
配置完成之后,即可通过
WebApplicationContextUtils.getWebApplicationContext方法在Web应用中获取ApplicationContext引用。
如:
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext();
LoginAction action=(LoginAction)ctx.getBean("action");
|
启动时会自动加载ApplicationContext实例。在web应用中虽然几乎所有被BeanFactory管理的用户代码都不需要知道BeanFactory, 但是在内部实现中,BeanFactory还是以某种方式实例化。对于独立的引用程序或者一些Servlet,action,jsp可以使用以下的方法实例化BeanFactory:
Resource resource = new FileSystemResource("beans.xml");
BeanFactoryfactory = new XmlBeanFactory(resource);
|
ClassPathResource resource = new ClassPathResource("beans.xml");
BeanFactoryfactory = new XmlBeanFactory(resource);
|
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
BeanFactory factory = (BeanFactory) context;
|
==================================================================================
但是,对于系统中非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那是轻而易举的事!
相关推荐
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class文件,动态实现spring bean 的动态加载,并通过UrlClassLoader完成jar和class文件的加载。可以实现jar的热替换。spring的bean动态加载则需要对...
Spring bean 管理大全 全面的介绍springbean各种操作原理
spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean
Spring Bean创建初始化流程
spring自动扫描和管理Bean的示例
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
spring动态向容器中添加bean和删除指定bean,不需要重启应用
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习...
spring创建bean简单示例
Spring 的bean的作用域总结,详细的总结了 Spring 的bean的作用域
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
spring管理bean应用实例代码;spring管理bean应用实例代码
Spring中关于Bean的管理的课件,可以在讲课的时候用它。
初始化后可访问Spring管理的Bean
Spring IOC和DI概述,Bean的配置形式,IOC容器BeanFactory和ApplicationContext概述,依赖注入的方式,属性注入,构造器注入等案例
spring bean 属性总结
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
bean文件 springbean文件
spring-bean 源码依赖的jar包,用于spring源码部署的时候使用