1.配置文件:
配置文件有两种(开头均是application,主要是文件的后缀):
作用:修改springboot自动配置的默认值
位置:
配置文件放在src/main/resourcr目录或者 类路径/config 下
2.YAML:
YAML(YAML Ain't Markup Language)
YAML A Markup Language:是一个标记语言
YAML isn't Markup Language:不是一个标记语言;
标记语言:
以前的配置文件;大多都使用的是 xxxx.xml文件;
YAML:以数据为中心,比json、xml等更适合做配置文件;
该语法风格:
server:
port:
使用语法:
k:(空格)v:表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
值的写法:
字面量:普通的值(数字,字符串,布尔)
k: v:字面直接来写;
字符串默认不用加上单引号或者双引号;
" ":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
' ':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n
1.对象、Map(属性和值)(键值对):
注意空格
Person:
name:xxxx
age:
行内写法:
Person: {name:xxx,age=}
2.数组(List、Set):
用-(空格) 值表示数组中的一个元素
行内写法
gender: [gril,boy]
导入配置文件处理器,配置文件进行绑定就会有提示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3.代码测试
//将配置文件中的每一个属性的值映射到这个组件中
//告诉springboot将本类中的所有属性和配置文件中的相关配置进行绑定
//(prefix = "person")将配置文件中以person下的所有属性进行绑定
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private boolean boss;//布尔值
private Date bir;//时间 private Map<String,Object> map;//Map
private List<String> lists;//List private Dog dog;//对象
}
public class Dog {
private String name;
private Integer age;
}
在yml配置文件中
person:
name: Mr
age:
boss: true
bir: //
map: {m1: v1,m2: v2}
lists:
- mc
- mt
dog:
name: dogdog
age:
测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
} }
Person{name='Mr', age=, boss=true, bir=Fri Dec :: CST , map={m1=v1, m2=v2},
lists=[mc, mt], dog=Dog{name='dogdog', age=}}
使用properties后缀的:
person.name=mr
person.age=
person.bir=//
person.boss=true
person.map.q1=1
person.map.q2=2
person.lists=a,b,c
person.dog.name=cat
person.dog.age=
关于乱码的问题:
properties配置文件在idea中默认utf-8可能会乱码
4.配置文件值注入@Value
@Value:1.字面量 2.#{spel} 3.${key}从环境变量配置文件中取值
@Value获取值和@ConfigurationProperties获取值比较
松散语法绑定:last_name = last-name = lastName 他们取的值都是相同的
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
数据校验:
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
private String name;
...
}
获取配置文件中的某个值:
@ResponseBody
@Controller
public class Helloword { @Value("${person.name}")
private String name; @RequestMapping("/hello")
public String hello(){
return "Hello tow!" + name;
}
}
5@PropertySource,@ImportResource
@PropertySource(value = {"classpath:/person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private boolean boss;
private Date bir;
...
}
@ImportResource
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:spring.xml"})
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
public class helloword {
public void hello(){
System.out.println("hello");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.cr.springboot01.ImportSoource.helloword"></bean>
</beans>
@Autowired
ApplicationContext app;
@Test
public void testImportSourcr(){
boolean h = app.containsBean("hello");
System.out.println(h);
}
@Bean
全注解方式:
新建一个配置类
//配置类,指明当前类使配置类,代替xml配置文件
@Configuration
public class MyConfig {
//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
@Bean
public Helloword hello(){
System.out.println("注解的方式");
return new Helloword();
}
}
@Autowired
ApplicationContext app;
@Test
public void testImportSourcr(){
boolean h = app.containsBean("hello");
System.out.println(h);
}