分析Spring的BeanFactory与ApplicationContext的getBean方法-----区别

最近做项目,做测试时,无意中遇到了一个问题:分别用BeanFactory和用ApplicationContext的getBean方法获取dataSource,用ApplicationContext获取成功,而用BeanFactory获取则报错

 

Java代码 复制代码  收藏代码
  1. public class DataSourceTest {   
  2.        
  3.     public static void main(String[] args) {   
  4.         ApplicationContext ctx = new ClassPathXmlApplicationContext("resources/spring/applicationContext-hibernate.xml");   
  5.         DataSource ds = (DataSource) ctx.getBean("dataSource");   
  6.         System.out.println(ds);   
  7.                 }   
  8. }  
public class DataSourceTest {
	
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("resources/spring/applicationContext-hibernate.xml");
		DataSource ds = (DataSource) ctx.getBean("dataSource");
		System.out.println(ds);
                }
}

 输出结果:org.springframework.jdbc.datasource.DriverManagerDataSource@1af33d6

 

Java代码 复制代码  收藏代码
  1. public class JdbcTemplateTest {   
  2.     static String delimiter = File.separator;   
  3.     static String classpath = "resources"+delimiter+"spring"+delimiter+"applicationContext-hibernate.xml";   
  4.            
  5.     public static void main(String[] args) {   
  6.         BeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath));   
  7.         DataSource ds = (DataSource) factory.getBean("dataSource");   
  8.         /**  
  9.          * 以上方法获得DataSource的Bean会报错:说找不到数据库的驱动类  
  10.          *   
  11.          * */  
  12.         System.out.println(ds);   
  13.   
  14.     }   
  15. }  
public class JdbcTemplateTest {
	static String delimiter = File.separator;
	static String classpath = "resources"+delimiter+"spring"+delimiter+"applicationContext-hibernate.xml";
		
	public static void main(String[] args) {
		BeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath));
		DataSource ds = (DataSource) factory.getBean("dataSource");
		/**
		 * 以上方法获得DataSource的Bean会报错:说找不到数据库的驱动类
		 * 
		 * */
		System.out.println(ds);

	}
}

 运行报错:

Error creating bean with name 'dataSource' defined in class path resource [resources/spring/applicationContext-hibernate.xml]:

Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException;

nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception;

nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not load JDBC driver class [${jdbc.driverClassName}]; nested exception is java.lang.ClassNotFoundException: ${jdbc.driverClassName}

 

我的配置文件位置如下所示:

resouces包-----
                   |

                   |

               config子包:放数据库配置信息:jdbc.properties文件

               spring子包:放spring的配置文件:applicationContext.xml

两个文件的内容如下:

jdbc.properties文件内容:

 

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:LOCALDEV
jdbc.username=ccic
jdbc.password=ccic

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"  
  6.     default-autowire="byName">  
  7.     <!-- 属性文件读入 -->  
  8.     <bean id="propertyConfigurer"    
  9.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  10.         <property name="locations">  
  11.             <list>  
  12.                 <value>classpath:resources/config/jdbc.properties</value>  
  13.             </list>  
  14.         </property>  
  15.     </bean>  
  16.        
  17.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  18.         <property name="driverClassName" value="${jdbc.driverClassName}"/>  
  19.         <property name="url" value="${jdbc.url}" />  
  20.         <property name="username" value="${jdbc.username}" />  
  21.         <property name="password" value="${jdbc.password}" />  
  22.     </bean>  
  23. </beans>  

 

问题:同样是读取同一个配置文件,Spring的BeanFactory与ApplicationContext的getBean方法有什么区别?为什么一个行一个不行呢?

我只知道它们两个在延迟加载方面有区别:BeanFactory的getBean是延迟加载,ApplicationContext的getBean是在容器启动时就创建

 

 

 

特性                            BeanFactory  ApplicationContext
Bean实例化/装配                      Yes          Yes
自动BeanPostProcessor注册          No           Yes
自动BeanFactoryPostProcessor注册  No           Yes
便捷的 MessageSource访问(i18n)    No           Yes
ApplicationEvent发送               No           Yes

 

 

ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean
如果要在BeanFactory中使用,bean factory post-processor必须手动运行

Java代码 复制代码  收藏代码
  1. BeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath));   
  2. PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();   
  3. cfg.setLocation(new ClassPathResource("resources/config/jdbc.properties"));   
  4. cfg.postProcessBeanFactory(factory);   
  5. DataSource ds = (DataSource) factory.getBean("dataSource");     

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值