Spring应用的运行环境及属性配置综述

1.Spring Framework的Environment接口

Spring Framework核心模块中的org.springframework.core.env.Environment接口,表示Spring应用的运行环境,用以表示Spring应用的profile和properties。


2.Spring Framework的profiles

1) 定义profiles

Spring的profiles是Spring应用的properties集合,可以根据不同的运行环境将properties分组为profile,在每一个profile中为某属性配置不同的值。

在定义任何一个Spring bean时,我们都可以将其定义为属于某个profile的Spring bean,只要使用标注@Profile("a_profile_name")。如下所示:

@Configuration
@Profile("production")
public class MyConfigurationsInProduction {

    // ...

}

@Configuration
public class AppConfig {

    @Bean("dataSource")
    @Profile("default")
    public DataSource standaloneDataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:com/bank/config/sql/schema.sql")
            .addScript("classpath:com/bank/config/sql/test-data.sql")
            .build();
    }

    @Bean("dataSource")
    @Profile("production")
    public DataSource jndiDataSource() throws Exception {
        Context ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
    }
}

2) 设置活动profiles

在一个Spring应用中可以存在多个profiles,那么究竟那个(或哪几个)profiles有效呢,可以通过属性spring.profiles.active设置。

可以在应用代码中设置spring.profiles.active属性如下:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();

也可以在application.properties文件中配置spring.profiles.active属性如下:

spring.profiles.active=product
...

还可以在Spring应用的启动命令行中设置启动参数如下:

java ... --spring.profiles.active=product, mysqldb
注意,这里同时设置了多个active的profiles。


对于Spring Boot应用,还可以在启动应用之前,调用方法设置active的profiles。

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.setAdditionalProfiles("mangodb");
        SpringApplication.run(App.class, args);
    }
}

3) profile专有的属性文件

首先必须明确,任何Spring应用都可以采用基本的属性文件application.properties。除此之外,Spring应用还可以为不同的profile提供专有的属性文件,专有属性文件的命名规范为application-{myprofilename}.properties。如名为product的profile,其专有的属性文件为application-product.properties。

如果Spring应用启动时没有设置任何active的profile,这种情况下使用的专有属性文件为application-default.properties,即默认属性文件。

显然,profile专有的属性文件优先级高于application.properties文件。


3.Spring Framework的属性

1) 定义属性

有多种定义属性的方式,按照属性定义的优先级从高到低,主要有如下方式:

  • 应用启动的命令行参数
如java ... --server.port=80
  • 应用运行所在的操作系统环境变量
  • 应用中profile相关的application-{profile}.properties文件
  • 应用中profile无关的application.properties文件

2) 通过Environment对象引用属性

ApplicationContext ctx = new GenericApplicationContext();
Environment env = ctx.getEnvironment();
boolean containsFoo = env.containsProperty("a.prop.name");
System.out.println("Does my environment contain the 'a.prop.name' property? " + containsFoo);

3) 通过@PropertySource("classpath:/a/properties/file/path")注入Environment对象并引用属性

@Configuration
@PropertySource("classpath:/com/ericsson/myapp.properties")
public class AppConfig {
    @Autowired
    Environment env;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(env.getProperty("a.prop.name"));
        return testBean;
    }
}

4) 通过@Value("${a_property_name}")直接注入配置属性的值

@Component
public class MyBean {

    @Value("${a.prop.name}")
    private String name;

}


参考链接:

https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-environment

https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#boot-features-external-config

https://docs.spring.io/spring/docs/4.3.9.RELEASE/javadoc-api/overview-summary.html

https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/api/org/springframework/boot/SpringApplication.html


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值