`
wiselyman
  • 浏览: 2081079 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81086
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130134
社区版块
存档分类
最新评论

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");

    }

}

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


2
0
分享到:
评论
3 楼 wiselyman 2015-06-16  
FondaWu 写道
生产、测试环境配置如此简单方便!


使用spring boot 更简单
2 楼 FondaWu 2015-06-16  
生产、测试环境配置如此简单方便!
1 楼 1927105 2015-05-19  
不错~~~~

相关推荐

Global site tag (gtag.js) - Google Analytics