首页 技术 正文
技术 2022年11月10日
0 收藏 479 点赞 4,260 浏览 3047 个字

一、背景

为了更好的使用springboot,所以看一下application.yml配置这块。主要是看数据绑定这块。

主要参考:https://www.hangge.com/blog/cache/detail_2459.html

二、项目主要内容

1、controller

package com.shuimutong.learn.springboot.yml.controller;import com.alibaba.fastjson.JSON;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
public class HelloController { @GetMapping("/hello2")
public String hello2() {
return "Hello, Jack!";
}
}

2、启动类

package com.shuimutong.learn.springboot.yml;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class YmlApplication {
public static void main(String[] args) {
SpringApplication.run(YmlApplication.class, args);
}
}

三、开始使用application.yml

1、从新建一个application.yml开始

在resources目录下新建application.yml,并写入以下内容,

server:
port: 8081

启动服务,从日志看出端口变更为8081,访问url说明配置生效。

2、在application.yml中添加几个属性看看

1)application.yml增加内容

my:
name: Big大
age: 20
info: name:${my.name}--age:${my.age}

2)新建一个MyData类

package com.shuimutong.learn.springboot.yml.bean;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
@Data
public class MyData {
@Value("${my.name}")
private String name;
@Value("${my.age}")
private int age;
@Value("${my.info}")
private String info;
}

使用@Value注解绑定application.yml中的配置

3)HelloController增加对应的使用

    @Resource
private MyData myData; @GetMapping("/getData")
public MyData getMyData() {
return myData;
}

4)启动服务,并访问对应的url

{"name":"Big大","age":20,"info":"name:Big大--age:20"}

说明值绑定正常。

3、单个属性绑定太麻烦?试试对象绑定

1)application.yml增加内容

classroom:
clazz: 一年级
grade: 3班
seatNum: 30
courses:
- 语文
- 数学
- 英语
- 化学
- 体育
- 美术
students:
- name: 张三
age: 8
- name: 李四
age: 9

2)编写对应的bean,并添加注解

//Classroom类
package com.shuimutong.learn.springboot.yml.bean;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.List;@Data
@Component
@ConfigurationProperties(prefix = "classroom")
public class Classroom {
private String clazz;
private String grade;
private int seatNum;
private List<String> courses;
private List<Student> students;
}//Student类
package com.shuimutong.learn.springboot.yml.bean;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
public class Student {
private String name;
private int age;
}

3)controller添加对应的方法

    @Resource
private Classroom classroom; @GetMapping("/classInfo")
public Classroom getClassroomInfo() {
System.out.println("this is stu:" + JSON.toJSONString(student));
return classroom;
}

4)启动服务看看效果

{"clazz":"一年级","grade":"3班","seatNum":30,"courses":["语文","数学","英语","化学","体育","美术"],"students":[{"name":"张三","age":8},{"name":"李四","age":9}]}

符合预期。

4、配置太多,能不能拆成多个文件?

可以的!

1)增加application-classroom.yml文件

将内容从application.yml中抽出来。

2)修改application.yml

增加以下内容:

spring:
profiles:
active:
- classroom

配置那块你没看错,yml文件名一定要以“application-”开头。

3)启动程序验证

5、还有没有其他招数?

有,请移步git:https://github.com/shuimutong/spring_learn/tree/master/spring_boot/yml

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291