wjlvivid 2010-09-21 15:24
浏览 633
已采纳

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是在容器启动时就创建

 

  • 写回答

3条回答 默认 最新

  • iteye_19529 2010-09-21 21:47
    关注

    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题