一般需要在不同的環境(日常環境、性能測試環境、預發環境和生產環境等等)中配置不同的數據源,例如,在開發時非常適合使用嵌入式數據源、在QA環境中比較適合使用DBCP的BasicDataSource、在生產環境中則適合使用<jee:jndi-lookup>元素,即使用JNDI查詢數據源。
在Spring實戰3:裝配bean的進階知識一文中我們探討過Spring的bean-profiles特性,這里就需要給不同的數據源配置不同的profiles,Java配置文件的內容如下所示:
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;
@Configuration
public class DataSourceConfiguration {
@Profile("development")
@Bean
public DataSource embeddedDataSource() {
return new 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); //初始大小
ds.setMaxTotal(10); //數據庫連接池大小
return ds;
}
@Profile("production")
@Bean
public DataSource dataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("/jdbc/SpittrDS");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
return (DataSource)jndiObjectFactoryBean.getObject();
}
}
利用@Profile注解,Spring應用可以運行時再根據激活的profile選擇指定的數據源。在上述代碼中,當development對應的profile被激活時,應用會使用嵌入式數據源;當qa對應的profile被激活時,應用會使用DBCP的BasicDataSource;當production對應的profile被激活時,應用會使用從JNDI中獲取的數據源。
上述代碼對應的XML形式的配置代碼如下所示:
<?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="http://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.BasicDataSource"
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="/jdbc/SpittrDS"
resource-ref="true"/>
</beans>
<beans profile="development">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath*:schema.sql" />
<jdbc:script location="classpath*:test-data.sql" />
</jdbc:embedded-database>
</beans>
</beans>
建立好數據庫連接后,就可以執行訪問數據庫的任務了。正如之前提到的,Spring對很多持久化技術提供了支持,包括JDBC、Hibernate和Java Persistence API(API)。在下一小節中,我們首先介紹如何在Spring應用中使用JDBC書寫持久層。
https://yq.aliyun.com/articles/54079
