package com.dark.horse.itim; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController//这个注解是表示注入SpringBoot IOC容器,以Json字符串的形式返回给客户端 public class HelloController { @RequestMapping("/hello")//定位到网址 public String hello(){ return "hello,springboot"; } }
配置文件:
优先级properties>yml>yaml
相同部分,读取的是优先级高的,不同部分照样读(
eg:properties:name=123
yml:name=456
age=5
结果为name=123,age=5)
yaml:简洁,数据为核心
people:
name: lin //people.name=lin,,,,,,
age: 5 //peope.age=5
数据值后面记得加空格 eg:lin,霖,5....
缩进表层级关系
数组:
dog: - ko - jo
转义字符:
mag1: 'hello /n world' #结果:hello /n world 单引号不识别转义字符 mag2: "hello /n world" #结果: #hello #world 双引号识别转义字符
参数引用:
name: 张三
people:
name: ${name}
age: 5
@Value引入配置文件(适合导入少个)
package com.dark.horse.itim; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${people.name}") public String name; @Value("${people.age}") public String age; @Value("${age}") public String age1; @Value("${dog[0]}") public String dog1; @RequestMapping("/hello") public String hello(){ System.out.println(name);//张三 System.out.println(age);//5 System.out.println(age1);//30 System.out.println(dog1);//ko return dog1; } }
@Autowired(更方便一点,只要注入一个对象)
package com.dark.horse.itim; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private Environment env; @RequestMapping("/hello") public String hello(){ System.out.println(env.getProperty("dog[1]")); return "age"; } }
别导错包了

@ConfigurationProperties
中间类
package com.dark.horse.itim; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import java.util.Arrays; @Component @ConfigurationProperties(prefix = "people") public class person { private String name; private String age; private String[] cat; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String[] getCat() { return cat; } public void setCat(String[] cat) { this.cat = cat; } @Override public String toString() { return "person{" + "name='" + name + '\'' + ", age='" + age + '\'' + // ", cat=" + Arrays.toString(cat) + '}'; } }
package com.dark.horse.itim; org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello1") public String hello1(){ System.out.println(ps); String[] cat = ps.getCat(); for(String s:cat){ System.out.println(s); } return "ps"; } }
配置文件
people: name: ${name} age: 5 cat: - lin - li
本文来自博客园,作者:阿霖找BUG,转载请注明原文链接:https://www.cnblogs.com/lin-07/articles/16319404.html
浙公网安备 33010602011771号