从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象

例如:

配置文件:
sms.url=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
sms.appkey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
sms.secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
sms.signName=XXXXXXXXXXXXXXXXXXXXXXXXXXX
sms.tplCode=XXXXXXXXXXXXXXXXXXXXXXXXXXXX
sms.type=normal
JAVA代码:
@Component
@ConfigurationProperties(prefix = "sms")
public class SmsSettings { private String url ="";
private String appkey ="";
private String secret ="";
private String signName ="";
private String tplCode ="";
private String type ="";
private String open ="";
private String tplCode2 =""; public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAppkey() {
return appkey;
}
public void setAppkey(String appkey) {
this.appkey = appkey;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getSignName() {
return signName;
}
public void setSignName(String signName) {
this.signName = signName;
}
public String getTplCode() {
return tplCode;
}
public void setTplCode(String tplCode) {
this.tplCode = tplCode;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOpen() {
return open;
}
public void setOpen(String open) {
this.open = open;
}
public String getTplCode2() {
return tplCode2;
}
public void setTplCode2(String tplCode2) {
this.tplCode2 = tplCode2;
} }
通过注解@ConfigurationProperties来配置redis

@Configuration
@EnableAutoConfiguration
public class RedisConfig { @Bean
@ConfigurationProperties(prefix="spring.redis.poolConfig")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
} @Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
} @Bean
public RedisTemplate<?, ?> getRedisTemplate(){
RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
return template;
} }

Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms")的更多相关文章

  1. Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置

    从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...

  2. spring boot常用注解使用小结

    1.@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解. 4.0之前的版本,Sprin ...

  3. Spring Boot中注解@ConfigurationProperties

    在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...

  4. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  5. Spring Boot配置文件详解-ConfigurationProperties和Value优缺点-(转)好文

    文章转自 http://www.cnblogs.com/itdragon/p/8686554.html Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们 ...

  6. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  7. Spring Boot 常用注解汇总

    一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  8. 3个Spring Boot核心注解,你知道几个?

    Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...

  9. 【SpringBoot】15. Spring Boot核心注解

    Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...

随机推荐

  1. Array.prototype.forEach数组遍历

    forEach是Array新方法中最基本的一个,就是遍历,循环.先看以前是怎么遍历数组的 常用遍历 var arr = [1,2,3,4,5]; for(var i = 0; i < arr.l ...

  2. TextureMerger1.6.6 二:Sprite Sheet的制作和使用

    本随笔记录下Sprite Sheet的制作和使用 Sprite Sheet主要用于将零碎的小图合并成一张整图.减少加载图片时http的请求次数. 1 打开TextureMerger,选择Sprite ...

  3. 【Android】Scrollview 相关问题汇总

    去除Scrollview 滑动边界渐变颜色 去掉滚动条,并将在滑动时,边界不会变成灰白 <horizontalscrollview android:overScrollMode="ne ...

  4. 微信小程序 --- page.json文件

    page.json 文件用于配置当前目录.page.json文件里的配置可以修改 app.json 配置里面的 window:不能覆盖app.json文件里面的 tabBar / 网络超时/ debu ...

  5. Oracle Database Memory Structures

    Oracle Database creates and uses memory structures for various purposes. For example, memory stores ...

  6. 【转载】国外程序员整理的Java资源大全

    以下转载自: 推荐!国外程序员整理的Java资源大全中文版    https://github.com/akullpp/awesome-java英文版 Java 几乎是许多程序员们的入门语言,并且也是 ...

  7. js常用的几个验证

    /// <summary> ///1. 使用正则表达式验证 参数 是否 为数值 /// </summary> /// <param name="trNumber ...

  8. C++和C#的思考

    从2011年从业至今已经写了7年C++了,而C#.go语言虽然早有涉猎,但直到最近才开始思考语言的发展和工程之间的关系. C++ 更容易写出高内聚代码使用指针做原地内存操作直接堆栈控制,减少内存分配, ...

  9. Wireshark分析之TCP协议(二)

    (1)TCP首部格式 源端口:   用来传输数据报的端口 目标端口: 数据包将要发送到的端口 序号: 用来表示一个TCP片段.这个值用来表示数据流中的部分数据没有丢失 确认号:  表示通信中希望从另一 ...

  10. php基础:面向对象

    一.public.private.protected访问修饰符 public:任何都可以访问(本类.子类.外部都可以访问) protected:本类.子类都可以访问(本类.子类均可访问) privat ...