spring应用手册-IOC(XML配置实现)-(19)-beans标签的profile属性

戴着假发的程序员出品

beans标签的profile属性

spring应用手册(第一部分)


profile用于配置spring的多环境配置。

我们可以通过profile配置多个不同环境下的配置属性。

案例:

<?xml version="1.0" encoding="UTF-8"?>
<beans  default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <beans profile="test">
        <!-- 测试环境下的配置 -->
    </beans>
    <beans profile="prod">
        <!-- 生产环境下的配置 -->
    </beans>
</beans>

spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile。

ENV方式:ConfigurableEnvironment.setActiveProfiles(“test”)

JVM参数方式:set JAVA_OPTS="-Dspring.profiles.active=test"

web.xml方式:

<init-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>production</param-value>
</init-param>

标注方式(junit单元测试非常实用):

@ActiveProfiles({"unittest","productprofile"})

看案例:

我们配置如下,不同的环境下在service中注入不同的dao:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byType"  xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 注册accountDAO_mysql -->
    <bean id="accountDAO_mysql" autowire-candidate="false" class="com.dk.demo1.dao.impl.AccountDAO_mysql"/>
    <!-- 注册accountDAO_oracle -->
    <bean id="accountDAO_oracle"  class="com.dk.demo1.dao.impl.AccountDAO_oracle"/>
    <beans profile="mysql">
        <!-- 注册accountService  注入accountDAO_mysql -->
        <bean id="accountService" name="a_service"
               class="com.dk.demo1.service.AccountService">
            <property name="accountDAO" ref="accountDAO_mysql"/>
        </bean>
    </beans>
    <beans profile="oracle">
        <!-- 注册accountService  注入accountDAO_mysql -->
        <bean id="accountService" name="a_service"
              class="com.dk.demo1.service.AccountService">
            <property name="accountDAO" ref="accountDAO_oracle"/>
        </bean>
    </beans>
</beans>

测试:

@Test
public void testSpringProfile(){
    //创建spring容器
    ClassPathXmlApplicationContext ac =
            new ClassPathXmlApplicationContext("applicationContext.xml");
    //激活profile
    ConfigurableEnvironment environment = ac.getEnvironment();
    environment.setActiveProfiles("oracle");
    //重启容器
    ac.refresh();
    AccountService accountService =  ac.getBean(AccountService.class);
    accountService.save("戴着假发的程序员");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴着假发的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值