1. 使用@Value註解,能夠直接將屬性值注入到你的beans中,並經過Spring 的Environment抽象或綁定到結構化對象來訪問。spring
import org.springframework.stereotype.* import org.springframework.beans.factory.annotation.* @Component public class MyBean { @Value("${name}") private String name; // ... }
2. 配置隨機值less
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 dom
@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.Profilesthis
Spring Profiles提供了一種隔離應用程序配置的方式,並讓這些配置只能在特定的環境下生效。任何@Component或 @Configuration都能被@Profile標記,從而限制加載它的時機。spa
@Configuration @Profile("production") public class ProductionConfiguration { // ... }