首页 > 编程语言 > 详细

springboot 的常用操作

时间:2017-10-25 19:07:36      阅读:182      评论:0      收藏:0      [点我收藏+]

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 {
// ...
}

springboot 的常用操作

原文:http://www.cnblogs.com/jishan-coder/p/7731548.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!