Fork me on GitHub

springboot 的常用操作

1. 使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring 的Environment抽象或绑定到结构化对象来访问。

import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}

2. 配置随机值

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

3. @ConfigurationProperties 

@ConfigurationProperties(prefix="my")
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}

my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

4.Profiles

   Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。任何@Component或 @Configuration都能被@Profile标记,从而限制加载它的时机。

@Configuration
@Profile("production")
public class ProductionConfiguration {
// ...
}
posted @ 2017-10-25 18:38  纳兰性急  阅读(232)  评论(0编辑  收藏  举报