zoukankan      html  css  js  c++  java
  • 【Java Web开发学习】Spring环境profile

    【Java Web开发学习】Spring 环境profile

    转载:http://www.cnblogs.com/yangchongxing/p/8890702.html

    开发、测试、生产环境往往是不同的,我们需要将应用从一个环境迁移到另外一个环境,这时候就牵扯到不同环境配置是不同的。

    Spring提供了@Profile注解来指定bean属于哪一个profile

    1、配置@Profile注解,该注解可用于类也可用于方法

    用于类上只有当该profile是激活状态时,这个类的bean才能被创建

    package cn.ycx.web.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    import cn.ycx.web.model.Painter;
    import cn.ycx.web.model.Player;
    
    @Configuration
    @Profile("dev")
    public class TestConfig {
        @Bean
        public Player player() {
            return new Player();
        }
        @Bean
        public Painter painter() {
            return new Painter();
        }
    }

    用于方法上只有当该profile是激活状态时,标注@Bean的方法才能被创建bean

    package cn.ycx.web.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    import cn.ycx.web.model.Painter;
    import cn.ycx.web.model.Player;
    @Configuration
    public class TestConfig {
        @Bean
        @Profile("prod")
        public Player player() {
            return new Player();
        }
        @Bean
        @Profile("dev")
        public Painter painter() {
            return new Painter();
        }
    }

    2、激活profile

    Spring根据两个独立的属性来确定激活那个profile。spring.profiles.active和spring.profiles.default。spring.profiles.active比spring.profiles.default优先级高,当设置了spring.profiles.active属性值那么就用它的值来确定激活那个profile;只有当spring.profiles.active没有设置的时候才会去查找spring.profiles.default属性值并用它的值来确定激活那个profile。当两个值都没设置时就没有激活的profile。

    spring.profiles.active和spring.profiles.default可以为复数,多个值用逗号隔开。例如:spring.profiles.active=dev,play

    有多种方式来设置这两个值:

      1.作为DispatcherServlet的初始化参数

      2.作为Web应用的上下文参数

      3.作为JNDI条目

      4.作为环境变量

      5.作为JVM的系统属性

      6.在集成测试环境上使用@ActiveProfiles注解

    对于web应用个人喜欢在DispatcherServlet初始化参数和应用上下文参数指定spring.profiles.default值,通过环境变量指定spring.profiles.active值,这样就能在需要改变的地方轻松改变

      <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>prod</param-value>
      </context-param>
      <servlet>
        <servlet-name>ds</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <!-- 也可通过classpath指定路径 -->
          <param-value>WEB-INFds-servlet.xml</param-value>
        </init-param>
        <init-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>prod</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

    默认激活prod,通过下面的方式激活dev。

    选用环境变量

    windows系统

    注意:我的机器重启系统之后才生效了

    spring.profiles.active=dev

    ubuntu系统

    当前用户

    $ vim ~/.bashrc
    追加 export spring.profiles.active=dev
    $ source ~/.bashrc

    所有用户

    # vim /etc/profile
    追加 export spring.profiles.active=dev
    # source /etc/profile

    标注@Profile("dev")的Bean就会创建

  • 相关阅读:
    spring core源码解读之ASM4用户手册翻译之一asm简介
    nginx启动,重启,关闭命令
    linux LVM分区查看dm设备
    jdbc 对sqlite的基本操作
    linux配置多个ip
    细说Linux下的虚拟主机那些事儿
    打造字符界面的多媒体Linux系统
    linux计划crontab
    因修改/etc/ssh权限导致的ssh不能连接异常解决方法
    Linux修改主机名
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/8890702.html
Copyright © 2011-2022 走看看