springboot学习之配置入门
来源:互联网 发布:淘宝众筹成功怎么赚钱 编辑:程序博客网 时间:2024/04/29 04:45
一般新建springboot项目在src/main/resource下会有一个配置文件application.properties(如果没有,自己创建一个),我们可以在这个文件中自定义全局属性或对项目配置进行自定义。
application.properties或application.yml可以在以下几个位置:
外置,在相对于应用程序运行目录的/congfig子目录里。
外置,在应用程序运行的目录里
内置,在config包内
内置,在Classpath根目录按优先级排序,也就是说resources/config下的application.properties会覆盖resources下application.properties相同属性配置,同时,同一目录下.yml的优先级大于.properties
一、在application.properties中自定义属性并取值
1.在application.properties中
username="root"password="123"
2.创建一个controller类,通过注解@Value(value=”${config.name}”)就可以绑定到你想要的属性上面
@RestControllerpublic class TestController { @Value("${username}") private String username; @Value("${password}") private String password; @RequestMapping("/") //表示项目路径下路径 public String test(){ return "hello world"+username+password; }}
url访问:localhost:8080 发现成功绑定配置文件的属性值
二、自定义.properties配置文件
1.在resource目录下新建一个testapplication.properties文件并写上自定义属性
com.testvalue="我是自定义配置文件"
2.在controller类上使用注解@PropertySource(“classpath:test.properties”)指定读取配置文件路径
@RestController@PropertySource("classpath:test.properties")public class TestController2 { @Value("${com.testvalue}") private String test; @RequestMapping("test2") public String test(){ System.out.println(test); return test; }}
url访问:localhost:8080/test2
三、使用自定义数据封装类实现配置
如果配置的自定义的数据较多,可以针对的将一套的配置封装到configBean类中
1.创建一个TestConfigBean.java
/** * 如果不加这个注解也可以在Application类上加入注解 *@EnableConfigurationProperties({TestConfigBean.class}) */@Configuration @ConfigurationProperties(prefix = "testConfig")public class TestConfigBean { private String username="root"; private String password="123"; public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; }}
创建好config类后需要在application启动类上添加注解@EnableConfigurationProperties({TestConfigBean.class})指明加载哪个配置类,或者也可以直接在config类上加上注解@Configuration注明该类是注解类让程序启动的时候加载该类。
2.在其它类如果需要使用到配置类只需要直接使用@Autowired注解注入的方式就可以引用配置属性了
@RestControllerpublic class TestController { @Autowired TestConfigBean tcb; @RequestMapping("/test3") //表示项目路径下路径 public String test(){ return tcb.getUsername()+tcb.getPassword(); }}
url访问:localhost:8080/test3
四、使用自定义.properties结合configBean类
configBean类加载自定义.properties配置文件实现数据自动封装
1.在resource下创建testApplication.properties配置文件
com.user.name="陈"com.user.age=25com.user.work="搬砖工"
2.创建UserConfigBean数据实体类,加载配置文件并注解为配置类
@Configuration@ConfigurationProperties(prefix = "com.user")@PropertySource("classpath:test.properties")public class UserConfigBean { private String name; private int age; private String work; set and get ...
3.controller类直接使用注解注入的方式获得实体类
@RestControllerpublic class TestController2 { @Autowired private UserCongigBean user; @RequestMapping("test4") public String test(){ return user; }}
在application.properties中的各个参数之间也可以直接引用来使用,如定义
com.a="11",com.b="22"
com.c="11+22"
还可以定义com.c=${com.a}+${com.b}
源码下载:https://pan.baidu.com/s/1mij7ueG
这里附上大神笔记 : http://tengj.top/2017/04/24/springboot0/
- springboot学习之配置入门
- SpringBoot学习之入门篇
- SpringBoot入门之基本属性配置
- SpringBoot学习1之快速入门
- SpringBoot学习之——springboot的各种配置
- 【SpringBoot】SpringBoot之入门配置文件
- springboot学习9之admin监控配置
- springboot学习之拦截器配置
- SpringBoot 入门与配置
- springboot的入门学习
- SpringBoot 入门学习
- SpringBoot入门学习
- SpringBoot入门学习
- 《SpringBoot学习-入门》
- SpringBoot学习笔记 入门
- Springboot入门学习(一)
- SpringBoot入门之HelloWorld
- SpringBoot入门之HelloWorld
- 转载mongo数据库权限管理
- MPEG4码流简单分析
- JavaScript_DHTML
- @requestbody 和 @requestbody
- HTTP四种常见的POST提交数据方式
- springboot学习之配置入门
- java面向对象
- 有向图的邻接矩阵
- 【JOOQ】 数据查询修改demo
- Node.js抓数据(demo)
- linux下activeMQ安装
- jQuery
- 迷宫问题(java)
- 测试题目的答案