在开发一些项目中,我们可能会用到三种数据库配置,它们各有优劣。
嵌入式数据库:适合于开发环境
1 @Bean(destroyMethod="shutdown")
2 public DataSource dataSource() {
3 return new EmbeddedDataSourceBuilder()
4 .addScript("classpath:schema.sql")
5 .addScript("classpath:test-data.sql")
6 .build();
7 }
使用JNDI:适合于生产环境
1 @Bean
2 public DataSource dataSource() {
3 JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
4 jndiObjectFactoryBean.setJndiName("jdbc/myDS");
5 jndiObjectFactoryBean.setResourceRef(true);
6 jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
7 return (DataSource) jndiObjectFactoryBean.getObject();
8 }
使用DBCP连接池:适合于简单的集成和开发测试环境
1 @Bean(destroyMethod="close")
2 public DataSource dataSource() {
3 BasicDataSource dataSource = new BasicDataSource();
4 dataSource.setUrl("jdbc:h2:tcp://dbserver/~/test");
5 dataSource.setDriverClassName("org.h2.Driver");
6 dataSource.setUserName("sa");
7 dataSource.setPassword("password");
8 dataSource.setInitialSize(20);
9 dataSource.setMaxActive(30);
10 return dataSource;
11 }
为此,我们需要根据不同的情况,来装配不同的 dataSource 。
幸好,在3.1版本中,Spring引入了 bean profile 的功能,使用它,我们可以在运行时确定创建哪个bean和不创建哪个bean,而不是在构建的时候作出这样的决策。要使用 profile,首先要将所有不同的 bean 定义整理到一个或多个 profile 之中,在将应用部署到每个环境时,要确保对应的 profile 处于激活(active)的状态。
1 @Configuration
2 public class DataSourceConfig {
3 @Bean(destroyMethod="shutdown")
4 @Profile("dev")
5 public DataSource dataSource() {
6 return new EmbeddedDataSourceBuilder()
7 .addScript("classpath:schema.sql")
8 .addScript("classpath:test-data.sql")
9 .build();
10 }
11
12 @Bean
13 @Profile("prod")
14 public DataSource dataSource() {
15 JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
16 jndiObjectFactoryBean.setJndiName("jdbc/myDS");
17 jndiObjectFactoryBean.setResourceRef(true);
18 jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
19 return (DataSource) jndiObjectFactoryBean.getObject();
20 }
21 }
在这里,@profile 注解用在了方法级别上,与@Bean 注解一同使用。它会告诉 Spring 这个 bean 只有在该注解内声明的 profile 激活时才会被创建。(没有指定 profile 的 bean 始终都会被创建)
通过
1 <beans profile="dev">
2 <jdbc:embedded-database id="dataSource">
3 <jdbc:script location="classpath:schema.sql" />
4 <jdbc:script location="classpath:test-data.sql" />
5 </jdbc:embedded-database>
6 </beans>
同理,可以通过把 profile 设置为 prod ,创建适用于生产环境的从 JNDI 获取的 DataSource bean;也可以创建基于连接池定义的 dataSource bean,将其放在另一个XML文件中,并标注为 qa profile。所有的配置文件都会放在部署单元之中(如WAR文件),但是只有profile属性与当前激活的profile相匹配的配置文件才会被用到。
如果觉得定义的配置文件太多,你可以在根
1 <beans>
2
3 <beans profile="dev">
4 <jdbc:embedded-database id="dataSource">
5 <jdbc:script location="classpath:schema.sql" />
6 <jdbc:script location="classpath:test-data.sql" />
7 </jdbc:embedded-database>
8 </beans>
9
10 <beans profile="qa">
11 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
12 p:url="jdbc:h2:tcp://dbserver/~/test"
13 p:driverClassName="org.h2.Driver"
14 p:username="sa"
15 p:password="password"
16 p:initialSize="20"
17 p:maxActive="30" />
18 </beans>
19
20 <beans profile="prod">
21 <jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDatabase"
22 resource-ref="true" proxy-interface="javax.sql.DataSource" />
23 </beans>
24
25 </beans>
这里有三个 bean,类型都是 javax.sql.DataSource,并且 id 都是 dataSource。但是在运行时,只会创建一个 bean,这取决于处于激活状态的是哪个 profile。
那么,如何激活某个 profile 呢?
Spring在确定哪个 profile 处于激活状态时,需要依赖两个独立的属性:
如果设置了 spring.profiles.active 属性的话,那么它的值就会用来确定哪个 profile 是激活的。
如果没有设置 spring.profiles.active 属性的话,那Spring将会查找 spring.profiles.default 的值。
如果 active 和 default 都没有设置,那么就没有激活的 profile,因此只会激活那些没有定义在 profile 中的 bean。
设置属性激活的方式
例如,在 Servlet 上下文中进行设置:
1 <web-app>
2
3 <!-- 为上下文设置默认的profile -->
4 <context-param>
5 <param-name>spring.profiles.default</param-name>
6 <param-value>dev</param-value>
7 </context-param>
8
9 <!-- 为Servlet设置默认的profile -->
10 <servlet>
11 <servlet-name>appServlet</servlet-name>
12 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
13 <init-param>
14 <param-name>spring.profiles.default</param-name>
15 <param-value>dev</param-value>
16 </init-param>
17 <load-on-startup>1</load-on-startup>
18 </web-app>
集成测试时进行设置:
集成测试时使用@ActiveProfiles注解来指定测试时要激活的profile。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={PersistenceTestConfig.class})
@ActiveProfiles("dev")
public class PersistenceTest {
……
}
原文:https://www.cnblogs.com/miou/p/12251326.html