@ConfigurationProperties(prefix = “person”)的作用是告诉spring boot该类在配置文件中的前缀是什么,但是类的属性值的配置必须写在全局配置文件中,这样一来等到项目越来越庞大的时候,单靠一个全局配置文件就显得太过臃肿,所以@PropertySource注解应运而生。
@PropertySource注解:
向spring boot说明该类的配置文件的地址。
package com.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties: 告诉spring boot将本类中的所有属性和配置文件中相关的配置进行绑定
*
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
*/
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")//告诉spring boot该类在配置文件中的前缀是什么
//@Validated//说明以下数据需要数据校验
public class Person {
//@Email//lastName的值必须符合电子邮件的基本格式,否则会报错
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) {
this.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;
}
}
注意:
@PropertySource(value = {“classpath:person.properties”})和
@ConfigurationProperties(prefix = “person”),两个语句必须同时存在,否则要不报错,要不就获取不到对应的person值。value的值可以是数组形式的。
@ImportResource
是导入Spring配置文件,让配置文件里面的内容生效。
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,就需要把Spring的配置文件加载进入Spring Boot项目中,这就需要把@ImportResource注解标注在配置类上
package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = {"classpath:beans.xml"})//导入spring配置文件
@SpringBootApplication
public class SpringbootConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootConfigApplication.class, args);
}
}
标注在主启动类上。
<?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="helloService" class="com.springboot.service.HelloService"></bean>
</beans>
但是这并不是Spring Boot所提倡的
Spring Boot的推荐方式:推荐使用全注解的方式
- 配置类===Spring配置文件
- 使用@Bean给容器中添加组件
package com.springboot.config;
import com.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration//该注解指明当前类是一个配置类;就是来替代之前的Spring配置文件
public class MyAppConfig {
@Bean//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
public HelloService helloService(){
return new HelloService();
}
}