The first type: Beans.xml
XML code Collection Code
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource"
destroy-method= "Close" >
<property name= "Driverclassname" value= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name= "url"
Value= "Jdbc:sqlserver://localhost:1433;databasename=spring"/>
<property name= "username" value= "sa"/>
<property name= "Password" value= "********"/>
</bean>
Second type: Beans.xml
<bean id= "mappings"
XML code Collection code
class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
<property name=" Locations " Value= "Classpath:jdbc.properties" ></property>
</bean>
<bean id= "DataSource" class= " Org.apache.commons.dbcp.BasicDataSource "
destroy-method=" Close ">
<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>
Create a new jdbc.properties file in the SRC folder with the following contents:
XML code Collection code
Jdbc.driverclassname= Com.microsoft.sqlserver.jdbc.SQLServerDriver
Jdbcjdbc.url=jdbc:sqlserver://localhost:1433;databasename= Spring
Jdbc.username=sa
jdbc.password=********
Third type:
beans.xml
XML code Collection code
<bean id= "mappings"
class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
<property name=" Locations " Value= "Classpath:jdbc.properties" ></property>
</bean>
<bean id= "DataSource" class= " Org.apache.commons.dbcp.BasicDataSource "
destroy-method=" Close ">
<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>
<context:property-placeholder location= "Classpath:jdbc.properties"/> Create a new jdbc.properties file in the SRC folder with the following contents:
Jdbc.driverclassname=com.microsoft.sqlserver.jdbc.sqlserverdriver
jdbcjdbc.url=jdbc:sqlserver://localhost:1433;databasename=spring
Jdbc.username=sa
jdbc.password=**** ****
The second is similar to the third, except that the configuration file is specified differently.
Fourth type:
Beans.xml
XML code Collection code
<bean id= "mappings"
class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
<!--typed as a Java.util.Properties--
<property name= "Properties" >
<value>
jdbc.driverclassname= Com.microsoft.sqlserver.jdbc.SQLServerDriver
Jdbcjdbc.url=jdbc:sqlserver://localhost:1433;databasename= Spring
Jdbc.username=sa
jdbc.password=********
</value>
</property>
</bean> br>
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource"
destroy-method= "Close" >
<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>
The fifth type:
XML code Collection Code
Beans.xml
<bean id= "myDataSource" class= "Org.apache.commons.dbcp.BasicDataSource"
Destroy-method= "Close"
P:driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
P:url= "Jdbc:sqlserver://localhost:1433;databasename=spring"
P:username= "SA"
p:password= "********"/>
Plus namespaces:
XML code Collection Code
xmlns:p= "http://www.springframework.org/schema/p"
Performance parameters to determine how to configure the data according to the actual test results.
Transferred from: http://www.cppblog.com/fenglin/articles/130494.html
The sixth kind, recently posted on the Internet to see
An easy way to do this in
Pring3 is to context:property-placeholder/the element
simply by adding a
XML code collection code
<context to the spring configuration file: Property-placeholder location= "Classpath:jdbc.properties"/>
, where the location value is the position of the parameter configuration file, the parameter profile is usually placed in the SRC directory, The format of the parameter configuration file is the same as the Java generic parameter profile, which is the form of a key-value pair, for example:
#jdbc配置
Java Code Collection code
Test.jdbc.driverclassname= Com.mysql.jdbc.Driver
Test.jdbc.url=jdbc:mysql://localhost:3306/test
Test.jdbc.username=root
Test.jdbc.password=root
Line # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 1 For example, Spring has a JDBC data source class Drivermanagerdatasource
to define the bean in the config file:
Java code Collection code
<bean id= "Testdatasource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname" Value= "${test.jdbc.driverclassname}"/>
<property name= "url" value= "${test.jdbc.url}"/>
< Property name= "username" value= "${test.jdbc.username}"/>
<property name= "password" value= "${ Test.jdbc.password} "/>
</bean>
2. You can even use a variable of the form ${} in the annotations provided by spring to provide a value for the annotation's properties
Configuration of externalized Application parameters
Many of the parameter information that an application relies on during the development of an enterprise application, or when the enterprise application is deployed to a production environment, often needs to be adjusted, such as LDAP connections, RDBMS JDBC connection information. It is particularly important to manage this kind of information externally. Propertyplaceholderconfigurer and Propertyoverrideconfigurer objects, they are the responsibility of the external Configuration application parameters.
<context:property-placeholder/> elements
Propertyplaceholderconfigurer implements the Beanfactorypostprocessor interface, which can be externally managed for attribute values in <bean/>. Developers can provide a separate property file to manage related properties. For example, the existence of subordinate documents, excerpted from Userinfo.properties.
Properties Code Collection Code
Db.username=scott
Db.password=tiger
The following is excerpted from Propertyplaceholderconfigurer.xml. Normally, ${db.username}, ${db.password} and other similar information will not appear in the definition of userinfo. This uses propertyplaceholderconfigurer to manage the values of username and password attributes. Propertyplaceholderconfigurer modifies userinfo metadata information (<bean/> definition) before the DI container instantiates the userinfo. It replaces ${db.username} with the db.username corresponding to the Scott value in the userinfo.properties, Db.password corresponding tiger value to replace ${db.password}. Finally, when the Di container instantiates the userinfo, UserInfo will get the new attribute value instead of ${db.username}, ${db.password} and other similar information.
Java Code Collection code
<bean id= "propertyplaceholderconfigurer"
class= "Org.springframework.beans.factory.config .
Propertyplaceholderconfigurer >
<property name= "Locations" >
<list>
<value> Userinfo.properties</value>
</list>
</property>
</bean>
<bean name= " UserInfo "class=" test. UserInfo ">
<property name=" username "value=" ${db.username} "/>
<property name=" password "value=" $ {Db.password} "/>
</bean>
by running and analyzing the Propertyplaceholderconfigurerdemo sample app, Developers can understand propertyplaceholderconfigurer in depth. To simplify the use of Propertyplaceholderconfigurer, Spring provides <context:property-placeholder/> elements. The configuration example is given below, and when enabled, the developer does not have to configure the Propertyplaceholderconfigurer object.
<context:property-placeholder location= "Userinfo.properties"/>
Propertyplaceholderconfigurer built-in feature is very rich, if it does not find the XXX key defined in ${xxx}, it will also go to the JVM System Properties (System.getproperty () ) and Environment variables (system.getenv ()). By enabling the Systempropertiesmode and Searchsystemenvironment properties, developers can control this behavior.
There are 6 ways to configure DataSource in Spring3.