Bean Demo
package com.fanthe.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
//把普通pojo实例化到spring容器中,相当于配置文件中的
//<bean id="" class=""/>
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
/**
* 相当于
* <property name="lastName" value="${person.last-name}"></property>
*/
@Value("${person.last-name}")
private String lastName;
private Integer age;
private Date birth;
private Dog dog;
private List<Object> list;
private Map<String, Object> map;
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", birth=" + birth +
", dog" + dog.toString() +
", list=" + list +
", map=" + map +
'}';
}
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
@Value获取值与@ConfigurationProperties获取值的比较
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量注入配置文件中的属性 | 一个个指定 |
| 松散绑定(松散语法) | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303数据校验 | 支持 | 不支持 |
| 支持 | 不支持 |
@PropertySource与@ImportResource
@PropertySource:加载指定的配置文件
@ImportResource:加载Spring配置文件,让配置文件里面的内容生效
@ImportResource(locations = {"classpath:beans.xml"})
Spring-Boot推荐给容器中添加组件的方式 推荐使用全注解
1、配置类======Spring配置文件
2、使用@Bean给容器中添加组件
package net.huanju.springboottest.config;
import net.huanju.springboottest.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ Configuration:指明当前类是一个配置类,用于替代Spring配置文件
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中;组件的默认id为方法名
@Bean
public User user() {
return new User();
}
}
Spring-Boot配置文件加载位置:
-file:./config/
-file:../
-classpath:/config/
-classpath:/
优先级由高到低,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置加载主配置文件;互补配置
可以spring.config.location来改变默认配置文件的位置。
项目打包好以后,可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的配置文件共同起作用,形成互补配置
SpringBoot外部配置加载顺序
优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置
1、命令行参数
2、来自java:comp/env的JNDI属性
3、Java系统属性(System.getProperties())
4、操作系统环境变量
5、RandomValuePropertySource配置的random.*属性值
6、jar包外部的application-{profile}.properties或application.yml(带有spring.profile)配置文件
7、jar包内部的application-{profile}.properties或application.yml(带有spring.profile)配置文件
8、jar包外部的application.properties或application.yml(不带spring.profile)配置文件
9、jar包内部的application.properties或application.yml(不带spring.profile)配置文件
10、@Configuration注解类上的@PropertySource
11、通过SpringApplication.setDefaultProperties指定的默认属性
Spring Boot单元测试:
可以在测试期间很方便的类似编码一样自动注入
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootYmlconfigApplicationTests {
@Autowired
Person person;//测试的类
@Test
public void contextLoads() {
//测试操作
}
}