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/jdbc.properties "/> 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 Jdbc.properties 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= "${performance.jdbc.jndi.name}"/>
</bean>
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=http://www.springframework.org/schema/beans xmlns:xsi=http://www.w3.org/2001/ Xmlschema-instance Xmlns:jee=http://www.springframework.org/schema/jee xsi:schemalocation= "/http Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd " > <jee:jndi-lookup id= "DataSource" jndi-name= "JAVA:COMP/ENV/JDBC/BBT"/> </beans>
How the Spring framework obtains datasource objects