专栏名称: Guo_1_9
目录
相关文章推荐
51好读  ›  专栏  ›  Guo_1_9

Spring之旅第五站:数据库环境与profile、条件化的bean

Guo_1_9  · 掘金  ·  · 2018-02-23 08:22

正文

请到「今天看啥」查看全文


public DataSource jndiDataSource () { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName( "jdbc/myDS" ); jndiObjectFactoryBean.setResourceRef( true ); jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class); return (DataSource) jndiObjectFactoryBean.getObject(); } }

在Spring3.1,只能在类级别上使用@Profile注解,不过在Spring3.2开始,你也可以在方法级别上使用@Profile注解,与@Bean注解一同使用,这样的话,就能将两个bean的声明放在同一个配置类中。

/**
 * Created by guo on 22/2/2018.
 */
@Configuration
public class DataSourceConfig {
    @Bean(destroyMethod="shutdown")
    @Profile("dev")
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:schema.sql")
                .addScript("classpath:test-data.sql")
                .build();
    }
    @Bean
    @Profile("prod")
    public DataSource jndiDataSource() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName("jdbc/myDS");
        jndiObjectFactoryBean.setResourceRef(true);
        jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
        return (DataSource) jndiObjectFactoryBean.getObject();
    }

    @Bean(destroyMethod = "close")
    public DataSource dataSourceAO() {
        BasicDataSource   dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:h2:tcp://dbserver/~/test");
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUsername("guo");
        dataSource.setPassword("guo");
        dataSource.setInitialSize(20);
        dataSource.setMaxActive(30);
        return dataSource;
    }
}

尽管每个DataSource bean 都被声明在一个profile中,并且只能当规定的profile激活时,相应的bean才会被创建,但是可能会有其他的bean并没有声明到一个给定的profile范围内。 没有指定的profile的bean都会创建,与激活那个profile没有关系

** 在XMl中配置profile 我们也可以通过元素的profil属性,在XML中配置profile bean。

<?xml version="1.0" encoding="UTF-8"?>
<beans
  ................................................................
    <beans profile="dev">
        <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 profile="qa">
        <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close"
              p:url="jdbc:h2:tcp://dbserver/~/test"
              p:driverClassName="org.h2.Driver"
              p:username="guo"
              p:password="guo"
              p:initialSize="20"
              p:maxActive="39"/>
    </beans>

    <beans profile="prod">
        <jee:jndi-lookup id="dataSource"
                         lazy-init="true"
                         jndi-name="jdbc/myDatabase"
                         resource-ref="true"
                         proxy-interface="javax.sql.DataSource" />
    </beans>
</beans>

除了所有的bean定义到同一个XML文件中,这种配置方式与定义单独的XML文件中实际效果是一样的。在运行时,只会创建一个bean, 这取决于处于激活状态的是哪一个profile

3.1.2激活profile

Spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性: sring.profiles.active spring.profiles.default 。如果设置了 spring.profiles.active 属性的话,那么它的值就会用来确定哪个profile是激活的。但如果没有设置 spring.profiles.active 的话,那么Spring将会查找 spring.profiles.default 的值。如果两者都没有的话,那就没有激活的profile。

有多种方式来设置这两个属性

  • 作为DispatcherServlet的初始化参数
  • 作为Web的应用上下文参数
  • 作为JNDI条目
  • 作为环境变量
  • 作为JVM的系统属性
  • 在集成测试类上

作者喜欢的一种方式是使用DisPatcherServlet的参数将spring.profiles.default设置为开发环境,会在Servlet上下文中进行设置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>taotao-rest</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<!-- 加载spring容器 -->






请到「今天看啥」查看全文