SpringBoot(五):@ConfigurationProperties配置参数绑定
在springmvc或其他ssh框架中如果我们要实现一个配置参数的加载,需要使用代码实现读取properties文件等操作,或者需要使用其他属性@value(name="username")等配置操作。但是在springboot中就比较简单操作:
1)自定义配置参数绑定:通过使用@ConfigurationProperties和@Component注解自定义参数配置类,之后程序启动时将自动加载application.properties配置文件中的对应的配置项;
2)第三方组件类的配置参数绑定:需要在springboot启动类内部把该参数配置类注册为一个Bean,同时注解@ConfigurationProperties就可以实现第三方组件配置参数加载;
3)配置参数绑定启动参数:无论是上边1)还是2)参数配置除了可以在application.properties中配置外,还可以绑定启动参数。
1)自定义配置参数绑定:
a)创建自定义配置参数类:
在app下新建包config,在app.config包下创建一个MySQLConfig.java:
package app.config; import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @ConfigurationProperties(prefix = "db")
@Component
@Getter
@Setter
@ToString
public class MySQLConfig {
private String username;
private String password;
private String url;
private String driverClassName;
}
备注:上边注解@Getter@Setter@ToString的依赖包是lombok,需要在pom.xml添加配置:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
c)在src/resources/application.properties中添加配置:
db.username="root"
db.password="123456"
db.url="jdbc:mysql:///mytestdb"
db.driverClassName="com.mysql.jdbc.Driver"
备注:这里边的不区分大小写:db.driverClassName
可以写成:db.driverclassname=xx
可以写成:db.driver_class_name=xx
也可以写成db_driver-class_Name=xx
d)将app.config包注解为启动入口监控的组件包:
package app; import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans; import java.util.Arrays; @ComponentScans({@ComponentScan("com.dx.controller"), @ComponentScan("app.config")})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
System.out.println(Arrays.toString(args));
// 启动方式一:
SpringApplication.run(App.class, args); // 启动方式二:
// SpringApplication springApplication = new SpringApplication(App.class);
// springApplication.setBannerMode(Banner.Mode.OFF);
// springApplication.run(args); // 启动方式三:
// new SpringApplicationBuilder(App.class)
// .bannerMode(Banner.Mode.OFF)
// .build()
// .run(args);
}
}
e)新建测试接口类DataSourceTestController.java:
package com.dx.controller; import app.config.MySQLConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class DataSourceTestController {
@Autowired
private MySQLConfig mySQLConfig; @RequestMapping("/dataSource")
@ResponseBody
public String dataSource() {
System.out.println(mySQLConfig);
return "dataSource";
}
}
f)运行app.App.java,在浏览器中访问http://localhost:8888/dataSource回车,查看打印信息:
MySQLConfig(username="root", password="123456", url="jdbc:mysql:///mytestdb", driverClassName="com.mysql.jdbc.Driver")
2)第三方组件类的配置参数绑定:
a)假设上边自定义参数配置类app.config.MySQLConfig.java为一个第三方参数配置类:
package app.config; import lombok.Getter;
import lombok.Setter;
import lombok.ToString; @Getter@Setter@ToString
public class MySQLConfig {
private String username;
private String password;
private String url;
private String driverClassName;
}
b)在入口类中注册三方组件中参数配置类:
package app; import app.config.MySQLConfig;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans; import java.util.Arrays; @ComponentScans({@ComponentScan("com.dx.controller")})
@EnableAutoConfiguration
public class App {
@Bean
@ConfigurationProperties(prefix = "db")
public MySQLConfig mySQLConfig() {
return new MySQLConfig();
} public static void main(String[] args) {
System.out.println(Arrays.toString(args));
// 启动方式一:
SpringApplication.run(App.class, args); // 启动方式二:
// SpringApplication springApplication = new SpringApplication(App.class);
// springApplication.setBannerMode(Banner.Mode.OFF);
// springApplication.run(args); // 启动方式三:
// new SpringApplicationBuilder(App.class)
// .bannerMode(Banner.Mode.OFF)
// .build()
// .run(args);
}
}
c)测试:运行app.config.App.java,在浏览器中访问http://localhost:8888/dataSource,回车。查看打印信息:
MySQLConfig(username="root", password="123456", url="jdbc:mysql:///mytestdb", driverClassName="com.mysql.jdbc.Driver")
3)配置参数绑定启动参数:
打包项目为jar包,进入jar包生成目录执行:
java -jar jar包名称 --db.username=root --db.password=12345678 --db.url=jdbc:mysql:///mydb --db.driver=com.mysql.jdbc.Driver
执行后,访问地址http://localhost:8888/dataSource回车。此时,查看打印信息如下:
E:\Work\springboot\springboothelloword\target>java -jar springboot-helloword-1.0-SNAPSHOT.jar --db.username=root --db.password=12345678 --db.url=jdbc:mysql:///mydb --db.driver=com.mysql.jdbc.Driver
[--db.username=root, --db.password=12345678, --db.url=jdbc:mysql:///mydb, --db.driver=com.mysql.jdbc.Driver] _ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
2018-04-07 22:38:39.210 INFO 10288 --- [ main] app.App : Started App in 4.387 seconds (JVM running for 5.763)
2018-04-07 22:38:46.759 INFO 10288 --- [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-04-07 22:38:46.761 INFO 10288 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-04-07 22:38:46.805 INFO 10288 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 44 ms
MySQLConfig(username=root, password=12345678, url=jdbc:mysql:///mydb, driverClassName="com.mysql.jdbc.Driver")
SpringBoot(五):@ConfigurationProperties配置参数绑定的更多相关文章
- 对MySQL性能影响较大的五类配置参数
以下主要是对MySQL 性能影响关系紧密的五大配置参数的介绍. 一. 连接 连接通常来自Web 服务器,下面列出了一些与连接有关的参数,以及该如何设置它们. (一). ...
- (转)SpringMVC学习(五)——SpringMVC的参数绑定
http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...
- SpringBoot application.properties配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- 【Spring Boot】Spring Boot之自定义配置参数绑定到Java Bean
一.@Value方式 1.我的配置文件:application-dev.yml # 自定义项目配置 startproject: pro1: pro2: pro3: pro4: lists: - ' - ...
- 开发笔记-19/10/28 -SpringBoot @Value 获取配置参数
1. 在application.properties 定义参数 role.taskEvent :参数名称 4:值 ## ---------------------任务角色--------------- ...
- SpringBoot五步配置Mybatis
第一步:Maven里面添加mybatis的引用jar包: <!--mybatis--> <dependency> <groupId>org.mybatis.spri ...
- 2. springboot加载配置参数顺序
加载顺序依次是:1.jar的classes里面的application.properties 2.当前路径下config里面的application.properties 3.jar的classes里 ...
- SprimgMVC学习笔记(三)—— 参数绑定
一.默认支持的参数类型 1.1 需求 打开商品编辑页面,展示商品信息. 1.2 需求分析 编辑商品信息,首先要显示商品详情 需要根据商品id查询商品信息,然后展示到页面. 请求的url:/itemEd ...
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
随机推荐
- Java面试宝典-2017
Java面试宝典2017版 一. Java基础部分........................................................................... ...
- struts2和spring mvc的区别
在项目中使用struts2和spring mvc为了实现后台的业务代码和前台数据之间的传递,现在基本上不会有用struts2的了,几次面试问的最多的关于struts2的问题就是struts2和spri ...
- Android一个小巧的记录app(便签或者日记 随心)
入驻博客园两个月今天第一次发随笔,,话不多说,直接上图展示效果 主界面用的RecyclerView的瀑布流(StaggeredGridLayoutManager),同时加上Floatbutton悬浮按 ...
- Anagram
Anagram poj-1256 题目大意:给你n个字符串,求每一个字符串所有字符的全排列,按照顺序输出所有全排列. 注释:每一个字符长度小于13,且字符排序的顺序是:A<a<B<b ...
- Mysql加密解密随机函数
MD5(str) md5加密 SELECT MD5('hello') 5d41402abc4b2a76b9719d911017c592 sha(str) sha加密 SELECT SHA('hello ...
- python全栈学习--day9(函数初始)
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...
- python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数
一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...
- 记录python接口自动化测试--把操作excel文件的方法封装起来(第五目)
前面补充了如何来操作excel文件,这次把如何获取excel文件的sheet对象.行数.单元格数据的方法进行封装,方便后面调用 handle_excel.py# coding:utf-8 import ...
- 冲刺NO.12
Alpha冲刺第十二天 站立式会议 项目进展 项目核心功能,如学生基本信息管理模块,学生信用信息模块,奖惩事务管理模块等等都已完成,测试工作大体结束. 问题困难 项目结束后对项目的阶段性总结缺乏一定的 ...
- C语言--期末总结
一. 1.当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 答:当初报志愿的时候,没有具体的想法,只凭借着 ...