zoukankan      html  css  js  c++  java
  • Spring的profile属性

    使用示例

    //注解方式
    public class DataSourceConfig {
        @Bean
        @Profile("prod")    
        public DataSource dataSource(){
            return null;
        }
    }
    //xml方式
    <beans profile="prod">
        .............................
    </beans>

    属性含义

    通过profile标记不同的环境,可以通过设置spring.profiles.active和spring.profiles.default激活指定profile环境。如果设置了active,default便失去了作用。如果两个都没有设置,那么带有profiles的bean都不会生成。

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

    • 作为DispatcherServlet的初始化参数;
    • 作为web应用的上下文参数;
    • 作为JNDI条目;
    • 作为环境变量; System.set("spring.profiles.active","prod")
    • 作为JVM的系统属性; -Dspring.profiles.active="prod"
    • 在集成测试类上,使用@ActiveProfiles注解配置。

    以前两种方式举例,它们都可以在web.xml中设置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <context-param>
          <param-name>applicationContext</param-name>
          <param-value>/applicationContext.xml</param-value>
      </context-param>
      <!-- 在上下文中设置profile的默认值 -->
      <context-param>
          <param-name>spring.profiles.default</param-name>
          <param-value>dev</param-value>
      </context-param>
      
      <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
      </listener>
      
      <servlet>
          <servlet-name>appServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!-- 在servlet中设置profile的默认值 -->
          <init-param>
          <param-name>spring.profiles.default</param-name>
          <param-value>dev</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>appServlet</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
  • 相关阅读:
    洛谷 P2920 [USACO08NOV]时间管理Time Management
    湖南测试 1
    洛谷 P1824 进击的奶牛
    4、map 和 tuple
    -_-#【Better JS Code】插入迭代值
    -_-#【JS】defer / async
    -_-#【JS】HTML5 API
    【jQuery】jQuery API 过 一 遍
    ♫【函数】函数声明 / 函数表达式
    -_-#【Mac】命令
  • 原文地址:https://www.cnblogs.com/wade-luffy/p/6066932.html
Copyright © 2011-2022 走看看