一.@ImportResource原生配置文件注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
@ImportResource("classpath:beans.xml")
二.配置绑定 (@ConfigurationProperties)
1.需要加入容器
2.在需要访问的类中添加绑定会自动注册到容器中 (@EnableConfigurationProperties)
三.@SpringBootConfiguration(@Configuration代表是一个配置类)
由@ComponentScan(指定包扫描)和@EnableAutoConfiguration(自动配置包)
总结:
1.SpringBoot会自动加载所有配置类
2.配置类按照条件生效
3.生效的配置类中给容器提供组件
4.容器中由组件,就有了功能
自定义配置:用户自己用@bean替换组件修改默认的值
四.开发技巧
1.Lombok简化JavaBean开发
@Data加载get,set方法
2.简化日志开发(@Sl4j)
3.dev-tools快速构建
4.Spring Initailizr(项目初始化向导)快速构建SpringBoot项目
五.yaml配置文件
要点:
1.key: value kv之间有空格
2.大小写敏感
3.#注释
4.缩进表示同级关系
5.字符串不需要加引号
示例:
person:
username: zhangsan
boss: true
birth: 2001/08/15
age: 22
interests:
- 篮球
- 足球
#interests: [篮球,足球]
animals: [阿猫,阿狗]
score: {english:99,math:100}
salary:
- 999.99
- 999.98
pet:
name: 阿狗
weigth: 100
allPets:
sick:
- {name: 阿狗,weigth: 99.99}
- name: 阿毛
weigth: 88.88
health: [{name: 阿花,weight: 101},
{name: 阿呆,weight: 200}]