It is generally necessary to configure different data sources in different environments (daily environment, performance test environment, pre-set environment, production environment, etc.), for example, it is very suitable for the use of embedded data sources, in the QA environment for the use of DBCP Basicdatasource, In a production environment, it is appropriate to use the <jee:jndi-lookup> element, which is to query the data source using Jndi.
In Spring 3: The advanced knowledge of assembly beans we explored spring's bean-profiles feature, where the contents of different Profiles,java configuration files need to be configured for different data sources as follows:
Package Org.test.spittr.config;import Org.apache.commons.dbcp2.basicdatasource;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.profile;import Org.springframework.jdbc.datasource.drivermanagerdatasource;import Org.springframework.jdbc.datasource.embedded.embeddeddatabasebuilder;import Org.springframework.jdbc.datasource.embedded.embeddeddatabasetype;import Org.springframework.jndi.jndiobjectfactorybean;import Javax.sql.DataSource; @Configurationpublic class datasourceconfiguration {@Profile ("development") @Bean public DataSource Embeddeddatasource () {return NE W Embeddeddatabasebuilder (). SetType (EMBEDDEDDATABASETYPE.H2). Addscript ("Classpath*:schema. SQL "). Addscript (" Classpath*:test-data.sql "). Build (); } @Profile ("QA") @Bean public Basicdatasource Basicdatasource () {Basicdatasource Ds = new Basicdatasource (); Ds.setdriverclassname ("Org.h2.Driver"); Ds.seturl ("Jdbc:h2:tcp://localhost/~/spitter"); Ds.setusername ("sa"); Ds.setpassword (""); Ds.setinitialsize (5); Initial size ds.setmaxtotal (10); Database connection pool size return DS; } @Profile ("Production") @Bean public DataSource DataSource () {Jndiobjectfactorybean Jndiobjectfactorybea n = new Jndiobjectfactorybean (); Jndiobjectfactorybean.setjndiname ("/jdbc/spittrds"); Jndiobjectfactorybean.setresourceref (TRUE); Jndiobjectfactorybean.setproxyinterface (Javax.sql.DataSource.class); Return (DataSource) jndiobjectfactorybean.getobject (); }}
With @Profile annotations, the spring application can run and then select the specified data source based on the active profile. In the code above, when the development corresponding profile is activated, the application uses an embedded data source, and when the corresponding profile of the QA is activated, the application uses the DBCP Basicdatasource; When the production corresponding profile is activated, the application uses the data source obtained from Jndi.
The preceding code corresponds to the configuration code in the form of XML as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:jee= "H Ttp://www.springframework.org/schema/jee "xmlns:jdbc=" Http://www.springframework.org/schema/jdbc "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/ Spring-jdbc.xsd "> <beans profile=" QA "> <bean id=" dataSource "class=" org.apache.commons.dbcp2.b Asicdatasource "P:driverclassname=" Org.h2.Driver "p:url=" Jdbc:h2:tcp://localhost/~/spitter " P:username= "sa" p:password= "" p:initialsize= "5"/> </beans> <beans profile= "Production" > <jee:jndi-lookup id= "dataSource" Jndi-name= "/jdb C/spittrds "resource-ref=" true "/> </beans> <beans profile=" Development "> <jdbc:embedded-database id= "DataSource" type= "H2" > <jdbc:script location= "Classpath*:schem A.sql "/> <jdbc:script location=" Classpath*:test-data.sql "/> </jdbc:embedded-databa Se> </beans></beans>
Once the database connection is established, you can perform the task of accessing the database. As mentioned earlier, Spring provides support for many persistence technologies, including JDBC, hibernate, and Java Persistence APIs (APIs). In the next section, we first show you how to write a persistence layer in a spring application using JDBC.
https://yq.aliyun.com/articles/54079
Spring: Select a data source using profiles (combined with embedded data source Embeddeddatabasebuilder)