zoukankan      html  css  js  c++  java
  • 08点睛Spring4.1-Profile

    8.1 Profile

    • Profile让在不同环境下使用不同的配置提供了支持(如开发环境下的配置和生产环境下的配置肯定是不同的,如:数据库的配置);
      • 通过设定EnvironmentActiveProfiles来设定当前context需要使用的配置环境
      • 通过设定jvm的spring.profiles.active参数来设置配置环境(web项目中设置在servlet的context parameter中)

    8.2 示例

    8.2.1 新建测试bean

    package com.wisely.profile;
    
    
    public class DemoBean {
        private String url;
    
        public DemoBean(String url) {
            super();
            this.url = url;
            System.out.println("地址为:"+url);
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
    
    }
    

    8.2.2 编写配置文件

    package com.wisely.profile;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    @Configuration
    public class DemoConfig {
    
        @Bean
        @Profile("dev")
        public DemoBean devDemoBean(){
            return new DemoBean("http://www.baidu.com");
        }
    
        @Bean
        @Profile("prod")
        public DemoBean prodDemoBean(){
            return new DemoBean("http://www.qq.com");
        }
    
    }
    

    8.2.3 测试

    8.2.3.1 使用Environment选择配置

    package com.wisely.profile;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext();
            context.getEnvironment().setActiveProfiles("dev");
            context.scan("com.wisely.profile");
            context.refresh();
            context.close();
    
        }
    
    }
    

    输出结果

    地址为:http://www.baidu.com
    

    8.2.3.2 使用JVM参数选择配置

    package com.wisely.profile;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context =  new AnnotationConfigApplicationContext();
            context.scan("com.wisely.profile");
            context.refresh();
            context.close();
    
        }
    
    }
    

      输出结果:

    地址为:http://www.qq.com
    

    8.2.3.2 在web项目中的配置

    • web.xml(servlet 2.5及以下)
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>spring.profiles.active</param-name>
            <param-value>production</param-value>
        </init-param>
    </servlet>
    • java config(servlet 3.0及以上)
    public class WebInit implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext container) throws ServletException {
            container.setInitParameter("spring.profiles.default", "dev");
    
        }
    
    }
  • 相关阅读:
    克如斯卡尔 P1546
    真正的spfa
    第四课 最小生成树 要点
    关于vscode中nullptr未定义
    cmake学习笔记
    python学习笔记
    (BFS 图的遍历) 2906. kotori和迷宫
    (图论基础题) leetcode 997. Find the Town Judge
    (BFS DFS 并查集) leetcode 547. Friend Circles
    (BFS DFS 图的遍历) leetcode 841. Keys and Rooms
  • 原文地址:https://www.cnblogs.com/Jeely/p/11949868.html
Copyright © 2011-2022 走看看