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

 

 

 

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

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值