配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好
创建一个新的springboot项目,并创建Dog Person类
Person.java
package com.atguigu.springboot.bean;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Null;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix="person" :配置文件中的哪个下面的所有属性进行一一映射
* */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean Boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"lastName=‘" + lastName + ‘\‘‘ +
", age=" + age +
", Boss=" + Boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
‘}‘;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return Boss;
}
public void setBoss(Boolean boss) {
Boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
Dog.java
package com.atguigu.springboot.bean; public class Dog { private String name; private Integer age; @Override public String toString() { return "Dog{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
配置application.yml
在pom文件导入依赖
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
启动主程序:
现在我们对application.properties进行配置,先把application.yml的内容注释掉
修改编码方式,因为有中文信息:
重新修改配置文件,改过配置后,配置文件的中文会变成乱码
编写测试类:
package com.atguigu.springboot; import com.atguigu.springboot.bean.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; /** * SpringBoot单元测试; * * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能 * */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot02ConfigApplicationTests { @Autowired Person person; @Autowired ApplicationContext ioc; @Test public void testHelloService(){ boolean b = ioc.containsBean("helloService02"); System.out.println(b); } @Test public void contextLoads() { System.out.println(person); } }
运行测试类
通过@value获取值,启动测试类,观察终端打印的信息
@ConfigurationProperties
|
@value | |
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定 | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
配置文件yml还是properties他们都能获取到值
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@value
如果说,我们专门编写一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties
我们新建一个controller类:
package com.atguigu.springboot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${person.last-name}") private String name; @RequestMapping("/sayHello") public String sayHello(){ return "Hello"+name; } }
启动主程序
访问地址 http://localhost:8081/sayHello
@PropertySource:加载指定的配置文件
我们创建一个person.properties配置文件
注释掉application.properties的person配置
指定配置文件:
运行测试类:
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效:
创建beans.xml配置文件
修改测试主程序:
package com.atguigu.springboot; import com.atguigu.springboot.bean.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; /** * SpringBoot单元测试; * * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能 * */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot02ConfigApplicationTests { @Autowired Person person; @Autowired ApplicationContext ioc; @Test public void testHelloService(){ boolean b = ioc.containsBean("helloService"); System.out.println(b); } }
运行测试类:
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
运行测试类:
SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式
1、配置类=====Spring配置文件
2、使用@Bean给容器中添加组件
注释掉
创建MyAppConfig类
package com.atguigu.springboot.config; import com.atguigu.springboot.service.HelloService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Configuration:指明当前类是一个配置类;就是来替代之前Spring配置文件 * */ @Configuration public class MyAppConfig { //将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名 @Bean public HelloService helloService(){ System.out.println("配置类@Bean给容器中添加组件了。。。"); return new HelloService(); } }
运行测试主程序:
原文:https://www.cnblogs.com/braveym/p/14874579.html