Analyze the getbean method of beanfactory and applicationcontext in spring -- Difference

Source: Internet
Author: User

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
 
  1. Public class datasourcetest {
  2. Public static void main (string [] ARGs ){
  3. Applicationcontext CTX = new classpathxmlapplicationcontext ("resource/spring/applicationContext-hibernate.xml ");
  4. Datasource DS = (datasource) CTX. getbean ("datasource ");
  5. System. Out. println (DS );
  6. }
  7. }
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
 
  1. Public class jdbctemplatetest {
  2. Static string delimiter = file. Separator;
  3. Static string classpath = "Resources" + delimiter + "Spring" + delimiter + "applicationContext-hibernate.xml ";
  4. Public static void main (string [] ARGs ){
  5. Beanfactory factory = new xmlbeanfactory (New classpathresource (classpath ));
  6. Datasource DS = (datasource) Factory. getbean ("datasource ");
  7. /**
  8. * 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.
     
  9. *
  10. **/
  11. System. Out. println (DS );
  12. }
  13. }
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
 
  1. <? XMLVersion = "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. <! -- Read attribute file -->
  8. <BeanId = "propertyconfigurer"
  9. Class = "org. springframework. Beans. Factory. config. propertyplaceholderconfigurer">
  10. <PropertyName = "locations">
  11. <List>
  12. <Value>Classpath: Resources/config/jdbc. Properties</Value>
  13. </List>
  14. </Property>
  15. </Bean>
  16. <BeanId = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
  17. <PropertyName = "driverclassname" value = "$ {JDBC. driverclassname }"/>
  18. <PropertyName = "url" value = "$ {JDBC. url }"/>
  19. <PropertyName = "username" value = "$ {JDBC. Username }"/>
  20. <PropertyName = "password" value = "$ {JDBC. Password }"/>
  21. </Bean>
  22. </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
 
  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 ");

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.