When I was doing a test in a recent project, I accidentally encountered a problem: I used beanfactory and the getbean method of applicationcontext to get datasource, and applicationcontext to get datasource. However, if I used beanfactory to get datasource, I reported an error.
Java code
- Public class datasourcetest {
- Public static void main (string [] ARGs ){
- Applicationcontext CTX = new classpathxmlapplicationcontext ("resource/spring/applicationContext-hibernate.xml ");
- Datasource DS = (datasource) CTX. getbean ("datasource ");
- System. Out. println (DS );
- }
- }
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 );}}
Output result: org. springframework. JDBC. datasource. drivermanagerdatasource @ 1af33d6
Java code
- 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 ");
- /**
- * If the bean whose data source is obtained using the preceding method, an error is returned: The driver class of the database cannot be found.
- *
- **/
- System. Out. println (DS );
- }
- }
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");/*** the bean obtained from the above method will return an error: the driver class of the database cannot be found. ***/system. out. println (DS );}}
Running error:
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: cocould not load JDBC driver class [$ {JDBC. driverclassname}]; Nested exception is Java. lang. classnotfoundexception: $ {JDBC. driverclassname}
The location of my configuration file is as follows:
Resouces package -----
|
|
Config sub-package: Database Configuration Information: JDBC. properties File
Spring sub-package: Spring configuration file: applicationcontext. xml
The content of the two files is as follows:
JDBC. properties file content:
JDBC. driverclassname = oracle. JDBC. Driver. oracledriver
JDBC. url = JDBC: oracle: thin :@ localhost: 1521: localdev
JDBC. Username = CCIC
JDBC. Password = CCIC
XML Code
- <? XMLVersion = "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">
- <! -- Read attribute file -->
- <BeanId = "propertyconfigurer"
- Class = "org. springframework. Beans. Factory. config. propertyplaceholderconfigurer">
- <PropertyName = "locations">
- <List>
- <Value>Classpath: Resources/config/jdbc. Properties</Value>
- </List>
- </Property>
- </Bean>
- <BeanId = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
- <PropertyName = "driverclassname" value = "$ {JDBC. driverclassname }"/>
- <PropertyName = "url" value = "$ {JDBC. url }"/>
- <PropertyName = "username" value = "$ {JDBC. Username }"/>
- <PropertyName = "password" value = "$ {JDBC. Password }"/>
- </Bean>
- </Beans>
<? XML version = "1.0" encoding = "UTF-8"?> <Beansxmlns = "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"> <! -- Read attribute file --> <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>
Q: What is the difference between spring beanfactory and applicationcontext's getbean method when reading the same configuration file? Why can't one line work?
I only know that the two of them have differences in delayed loading: beanfactory's getbean is delayed loading, and applicationcontext's getbean is created when the container is started.
Beanfactory applicationcontext
Bean instantiation/Assembly Yes
Automatic beanpostprocessor Registration No Yes
Automatic beanfactorypostprocessor Registration No Yes
Convenient messagesource access (i18n) No Yes
Applicationevent sends no Yes
Applicationcontext can automatically identify and deploy beanfactorypostprocessor bean on the application.
To be used in beanfactory, Bean Factory post-processor must be manually run
Java code
- 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 ");