加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

把配置中的内容写到一个对象里面去@Component@ConfigurationProperties

(2022-08-15 14:24:56)
分类: springboot

package com.example.initializr_test.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping(value = "/test2")

@Component
//@Component注解表示将类交给spring作为bean进行管理;
@ConfigurationProperties(prefix = "girl")
//@ConfigurationProperties(prefix = "girl")注解表示读取application.yml文件中前缀为girl的配置

public class Test2Controller {

private String cupSize;

private Integer age;

public String getCupSize() {
return cupSize;
}

public Integer getAge() {
return age;
}

public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}

public void setAge(Integer age) {
this.age = age;
}

@GetMapping("/outputinfo")
public void outputinfo(){

System.out.println("cupSize:"+cupSize);

System.out.println("age:"+age);

}

}


application.yml

#冒号之后若有内容,需要有一个空格,这是yml规定的格式。

server:
port: 80
servlet:
context-path: /lizhengtonghe

#可以看到:在yml配置文件中并不需要指定属性的具体类型,只需要在注入的地方指定具体的类型即可
com.didispace.test.name: "zhuzaiming"
com.didispace.test.title: "springboot initializr"
test_dict: "com.didispace.test.name: ${com.didispace.test.name},com.didispace.test.title: ${com.didispace.test.title}"

girl:
cupSize: B
age: 18

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有