SpringBoot读取配置文件注入到配置类
2017-11-10 11:48
501 查看
SpringBoot介绍
springboot可支持你快速实现resutful风格的微服务架构,对开发进行了很好的简化springboot快速入门
http://projects.spring.io/spring-boot/#quick-start
1.在导入maven和配置springboot入口之后就能使用springboot了
2.项目需符合springboot结构要求,需要显示JSP页面时要配置JSP支持和在resource下创建application.properties文件中配置springmvc的前缀和后缀
正文
1.使用application.properties文件在文件中配置你的配置信息如下(由于最近在做微信支付,配置文件用微信举例)
wxpay.appID=wxd678efh567hg6787 wxpay.mchID=1230000109 wxpay.key=5K8264ILTKCH16CQ2502SI8ZNMTM67VS
配置类如下,在设置prefix之后通过setter注入到类中
@Configuration @ConfigurationProperties(prefix = "wxpay") //与配置文件中开头相同 public class MyConfig{ private String appID; private String mchID; private String Key; // 省略get set方法 }
2.使用其他配置
假如创建配置文件 conf.properties内容与上文相同
配置类
@Configuration // 配置文件路径 @ConfigurationProperties(prefix = "wxpay",locations="classpath:config/conf.properties") public class MyConfig2{ private String appID; private String mchID; private String Key; // 省略get set方法 }
3.最后在springboot的入口类上加上
@EnableConfigurationProperties(MyConfig.class,MyConfig2.class)
@SpringBootApplication @EnableConfigurationProperties({MyConfig.class,MyConfig2.class}) public class Application { public static void main(String[] args) { SpringApplication.run(Application .class, args); } }
相关文章推荐
- Springboot 读取配置文件
- spring boot 中配置文件的读取和多环境配置
- SpringBoot 读取 .yml配置文件
- Spring Boot的properties配置文件读取
- spring-boot读取application配置文件中字段
- SpringBoot之十二:SpringBoot读取配置文件
- Spring-boot中读取config配置文件的两种方式
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- springboot读取配置文件
- Spring依赖注入的XML配置文件的实现思想(1)——准备工作:如何读取XML的文件的内容
- springboot(一)启动和配置文件的读取
- Spring Boot的properties配置文件读取
- spring boot 读取配置文件(application.yml)中的属性值
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Springboot读取配置文件及自定义配置文件的方法
- spring-boot读取props和yml配置文件的方法
- springboot学习笔记2(拦截器,redis,授权登录,读取yml配置文件)
- Spring-boot中读取config配置文件的两种方式
- Spring boot之读取自定义配置文件
- spring-boot中的读取配置文件