Spring的BeanFactory与ApplicationContext的getBean方法有什么区别?

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

 

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

 

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 version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    default-autowire="byName">
    <!-- 属性文件读入 -->
    <bean id="propertyConfigurer" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:resources/config/jdbc.properties</value>
            </list>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
</beans>

 

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

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

 

[table]
特性|BeanFactory|ApplicationContext
Bean实例化/装配|Yes|Yes
自动BeanPostProcessor注册|No|Yes
自动BeanFactoryPostProcessor注册|No|Yes
便捷的MessageSource访问(i18n)|No|Yes
ApplicationEvent发送|No|Yes
[/table]
ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean
如果要在BeanFactory中使用,bean factory post-processor必须手动运行
[code="java"]
BeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new ClassPathResource("resources/config/jdbc.properties"));
cfg.postProcessBeanFactory(factory);
DataSource ds = (DataSource) factory.getBean("dataSource");

[/code]

那个,不会用表格标签,表格错位了
特性 BeanFactory ApplicationContext
Bean实例化/装配 Yes Yes
自动BeanPostProcessor注册 No Yes
自动BeanFactoryPostProcessor注册 No Yes
便捷的 MessageSource访问(i18n) No Yes
ApplicationEvent发送 No Yes

[size=medium][color=green]解释清楚明了[/color][/size]