How the Spring framework acquires DataSource objects

Source: Internet
Author: User

1, using Org.springframework.jdbc.datasource.DriverManagerDataSource

2. Using Org.apache.commons.dbcp.BasicDataSource

3. Using Org.springframework.jndi.JndiObjectFactoryBean
Summary: The first of 3 ways is not used in the connection pool, so less in the project, the third way to configure the data source in the Web server, not easy to deploy, I recommend the use of every two ways to configure the data source.

Read the configuration file by referencing the properties:

<bean id="propertyconfigurer"   2.  Class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >   3.    <property name="location" value="/web-inf/  "/>   4.</bean>   

<bean id="DataSource" class="Org.apache.commons.dbcp.BasicDataSource"   2. destroy-method="Close">3. <property name="Driverclassname"Value="${jdbc.driverclassname}"/>4. <property name="URL"Value="${jdbc.url}"/>5. <property name="username"Value="${jdbc.username}"/>6. <property name="Password"Value="${jdbc.password}"/>7.</bean>

To define a property value in the property file:     jdbc.url= jdbc:mysql://localhost:3309/sampledb     jdbc.username= root     jdbc.password=    

Tip developers often accidentally type some spaces before and after the ${xxx}, and the space characters are merged with the variable as the value of the property. such as: <property name= "username" value= "${jdbc.username}" ></property> of the property configuration items, before and after the space, after being parsed, the value of username is "1234 "This will cause the final error and therefore requires special care.

Get Jndi Data source

If the application is configured on a high-performance application server, such as WebLogic or WebSphere, we may prefer to use the data source provided by the application server itself. The data source for the application server is used by Jndi open callers, andspring specifically provides the Jndiobjectfactorybean class that references the Jndi resource. The following is a simple configuration:

<bean id= "DataSource"  class= "Org.springframework.jndi.JndiObjectFactoryBean" >       < Property Name= "Jndiname" value= "JAVA:COMP/ENV/JDBC/BBT"/>        </bean>    

<bean id= "Performancedatasource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
<property name= "Jndiname" value= "${}"/>

Spring 2.0 provides a JEE namespace for acquiring the Java EE resource, which, through the Jee namespace, effectively simplifies the reference to the Java EE resource. The following is a configuration that uses the Jee namespace to reference a Jndi data source:

<beans Xmlns=      xmlns:xsi= Xmlschema-instance      Xmlns:jee=      xsi:schemalocation= "/http       Http:// " >            <jee:jndi-lookup id= "DataSource" jndi-name= "JAVA:COMP/ENV/JDBC/BBT"/>        </beans>    

How the Spring framework obtains datasource objects

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: 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.