在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配置参数绑定的更多相关文章

  1. 对MySQL性能影响较大的五类配置参数

    以下主要是对MySQL 性能影响关系紧密的五大配置参数的介绍. 一.      连接 连接通常来自Web 服务器,下面列出了一些与连接有关的参数,以及该如何设置它们. (一).             ...

  2. (转)SpringMVC学习(五)——SpringMVC的参数绑定

    http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...

  3. SpringBoot application.properties配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  4. 【Spring Boot】Spring Boot之自定义配置参数绑定到Java Bean

    一.@Value方式 1.我的配置文件:application-dev.yml # 自定义项目配置 startproject: pro1: pro2: pro3: pro4: lists: - ' - ...

  5. 开发笔记-19/10/28 -SpringBoot @Value 获取配置参数

    1. 在application.properties 定义参数 role.taskEvent :参数名称 4:值 ## ---------------------任务角色--------------- ...

  6. SpringBoot五步配置Mybatis

    第一步:Maven里面添加mybatis的引用jar包: <!--mybatis--> <dependency> <groupId>org.mybatis.spri ...

  7. 2. springboot加载配置参数顺序

    加载顺序依次是:1.jar的classes里面的application.properties 2.当前路径下config里面的application.properties 3.jar的classes里 ...

  8. SprimgMVC学习笔记(三)—— 参数绑定

    一.默认支持的参数类型 1.1 需求 打开商品编辑页面,展示商品信息. 1.2 需求分析 编辑商品信息,首先要显示商品详情 需要根据商品id查询商品信息,然后展示到页面. 请求的url:/itemEd ...

  9. (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解

    http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...

随机推荐

  1. PHP中的ArrayAccess用法详解

    在Laravel的源码当中,作者多次使用到了PHP SPL中的ArrayAccess接口,那么这个ArrayAccess接口到底有什么作用呢?我会用一个简单的例子跟大家说明. 请看下面的这段代码,Fo ...

  2. [Apio2010] 巡逻

    Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...

  3. Java 容器 接口

    Java 中容器框架的内容可以分为三层: 接口(模型), 模板和具体实现. 在开发中使用容器正常的流程是,首先根据需求确定使用何种容器模型,然后选择一个符合性能要求的容器实现类或者自己实现一个容器类. ...

  4. ASUS T100TA 换屏要记

    建议完整阅读后再执行操作! 参考: [图片]华硕T100换触摸屏详细教程,全网第一发[平板电脑吧]_百度贴吧 [图片]我是这么修T100的……换外屏[win8平板吧]_百度贴吧 淘宝信息: 选择适用型 ...

  5. SSRS: How to Display Checkbox on Report

    How to Display Checkbox on Report A textbox with Wingdings font type can be used to display a checkb ...

  6. 关于使用Mybatis的使用说明(一)【未完善待更新】

    (一)搭建Mybatis环境 (1)先导入常用的jar包:并且需要将lib文件夹下的包导入到项目中 (2)创建config文件夹,配置log4j.properties文件 # Global loggi ...

  7. Mybatis学习笔记一

    Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为M ...

  8. JavaWeb学习笔记三 Servlet

    Servlet 是运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API,动态的向客户 ...

  9. Java基础学习笔记十二 类、抽象类、接口作为方法参数和返回值以及常用API

    不同修饰符使用细节 常用来修饰类.方法.变量的修饰符 public 权限修饰符,公共访问, 类,方法,成员变量 protected 权限修饰符,受保护访问, 方法,成员变量 默认什么也不写 也是一种权 ...

  10. Java基础学习笔记二十 IO流

    转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStre ...