Spring教程08-环境配置之profile

阅读原文
作为开发人员我们经常会遇到不同环境下配置的切换问题,那么对于spring的配置我们又该如何去做切换呢?这将使用到Spring的 profile功能,它可以定义某个配置类或者Bean的使用环境,我们在容器加载之前只需要配置其需要激活的profile就可以完成不同环境下配置的随意切换。

1.1、使用profile定义配置类

1.1.1、基于JavaConfig

@Configuration
@Profile("dev")
public class DataSourceDevConfig {
    @Bean
    public DataSource embeddedDataSource() {
        EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
        //嵌入式数据库类型
        edb.setType(EmbeddedDatabaseType.H2);
        return edb.build();
    }
}

@Profile使用在类上时,表示该配置类只有在指定的环境下才生效。

1.1.2、基于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: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/jdbc
       http://www.springframework.org/schema/jdbc/spring-jdbc.xsd" profile="dev">
    <jdbc:embedded-database type="H2" id="embeddedDataSource"/>
</beans>

当我们在配置文件中这样配置时,表示该配配置文件只有在指定的环境下才生效。

1.2、使用profile定义Bean

1.2.1、基于JavaConfig

@Configuration
public class DataSourceConfig {
    @Bean
    @Profile("dev")
    public DataSource embeddedDataSource() {
        EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
        //嵌入式数据库类型
        edb.setType(EmbeddedDatabaseType.H2);
        return edb.build();
    }
    @Bean
    @Profile("test")
    public DataSource basicDataSource() {
        BasicDataSource ads = new BasicDataSource();
        ads.setDriverClassName("com.mysql.jdbc.Driver");
        ads.setUrl("jdbc:mysql://localhost:3306/myDb");
        ads.setUsername("root");
        ads.setPassword("123");
        ads.setMaxActive(10);
        ads.setMaxIdle(5);
        ads.setMinIdle(1);
        ads.setInitialSize(20);
        return ads; 
    }
    @Bean
    @Profile("pro")
    public DataSource dataSource() {
        JndiObjectFactoryBean jfb = new JndiObjectFactoryBean();
        jfb.setJndiName("jdbc/myDb");
        jfb.setResourceRef(true);
        jfb.setProxyInterface(javax.sql.DataSource.class);
        return (DataSource) jfb.getObject();
    }
}

当@profile使用到某个Bean上时,表示该Bean只有在指定的环境下才会被装配。

1.2.2、基于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:c="http://www.springframework.org/schema/c" 
	   xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util" 
	   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/util 
	   http://www.springframework.org/schema/util/spring-util.xsd 
	   http://www.springframework.org/schema/jdbc 
	   http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
	   http://www.springframework.org/schema/jee 
	   http://www.springframework.org/schema/jee/spring-jee.xsd">
    <beans profile="dev">
        <jdbc:embedded-database id="embeddedDataSource" type="H2"/>
    </beans>
    <beans profile="test">
        <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource"
              p:driverClassName="com.mysql.jdbc.Driver"
              p:url="jdbc:mysql://localhost:3306/myDb"
              p:username="root"
              p:password="123"
              p:maxActive="10"
              p:maxIdle="5"
              p:minIdle="1"
              p:initialSize="20">
        </bean>
    </beans>
    <beans profile="pro">
        <jee:jndi-lookup jndi-name="jdbc/myDb" resource-ref="true" 
proxy-interface="javax.sql.DataSource"/>
    </beans>
</beans>

配置文件的这种用法和JavaConfig作用相同,也是表明各个组件只有在特定的环境下才会被装配。

1.3、激活profile

激活profile时需要依赖两个独立的属性:spirng.profiles.active和spirng.profiles.default,前者的优先级大于后者。
激活上下文中的profile,在web.xml配置:

<context-param>  
	<param-name>spring.profiles.active</param-name> 
	<param-value>dev</param-value> 
</context-param>

激活servlet中的profile,在web.xml配置:

<servlet>
	<servlet-name>springMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/spring-mvc.xml</param-value>
	</init-param>
	<init-param>
		<param-name>spring.profiles.active</param-name> 
		<param-value>dev</param-value> 
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>springMVC</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

手动加载上下文时激活profile

public class TestDataSourceConfig {
private static AnnotationConfigApplicationContext afac = new 
AnnotationConfigApplicationContext();
    @Test
    public void testDataSourceConfig() throws SQLException {
        afac.getEnvironment().addActiveProfile("dev");
        afac.register(DataSourceConfig.class);
        afac.refresh();
    }
}

至此常用的环境配置与激活已全部探讨完毕。

更多最新技术文章,请关注“冰点IT”公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值