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

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 大学计算机网络实验报告
      • ¥20 Oracle触发器,用户管理题目
      • ¥250 python的exchangelib 报错
      • ¥15 Oracle外部过程获取的句柄属性为空
      • ¥20 snmpv3相关提问
      • ¥20 boost电路升压频率和占空比选择
      • ¥15 找个人工智能的大佬咨询一些毕设问题
      • ¥15 需要教程:GD32F450I移植无系统lwip
      • ¥15 中文语料库检索构式 求助
      • ¥15 关于naver点击量信息