一般需要在不同的环境(日常环境、性能测试环境、预发环境和生产环境等等)中配置不同的数据源,例如,在开发时非常适合使用嵌入式数据源、在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)。