SpringBoot中@PropertySource和@ImportResource注解的使用

@PropertySource:加载指定的配置文件
需要和@ConfigurationProperties()注解一起使用

@Component
@ConfigurationProperties(prefix = "person")
//@Validated
@PropertySource(value={"classpath:person.properties"})
public class Person
{

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件/#{SpEL}"></property>
     * <bean/>
     * 这里的value属性等于 @Value("hanjuwang")
     */
   // @Value("${person.last-name}")
   // @Email
    private String lastName;
    //@Value("#{11 * 2}")
    private Integer age;
   // @Value("true")
    private Boolean boss;
    private Date birth;
    //@Value("${person.maps}")
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    @Override
    public String toString()
    {
        return "Person{" +
                "age=" + age +
                ", boss=" + boss +
                ", lastName='" + lastName + '\'' +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }

    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 String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    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;
    }

}


SpringBoot中@PropertySource和@ImportResource注解的使用_第1张图片
SpringBoot中@PropertySource和@ImportResource注解的使用_第2张图片

@ImportResource :导入Spring的配置文件,让配置文件里面的内容生效
SpringBoot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别
想让Spring的配置文件生效,加载进来,把@ImportResource标注在一个配置类上

@ImportResource(locations = {"classpath:bean.xml"})
导入Spring的配置文件让其生效

SpringBoot中@PropertySource和@ImportResource注解的使用_第3张图片

SpringBoot中@PropertySource和@ImportResource注解的使用_第4张图片

1.不来编写Spring配置文件的方式

<?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.atguigu.springboot.service.HelloService"></bean>

</beans>

SpringBoot推荐给容器中添加组件的方式:推荐使用全注解的方式
1.配置类 ======== Spring的配置文件

 * @Configuration:指明当前类是一个配置类,就是来替代之前的Spring配置文件的
 *
 * 在配置文件中用<bean></bean>标签添加组件
 *
 */

@Configuration
public class MyAppConfig
{
    /**
     *  @Bean:将方法的返回值添加到容器中
     *  这个组件默认的id就是方法名
     * @return
     */
    @Bean
    public HelloService helloService(){
        System.out.println("配置类@Bean给容器中添加组件了");
        return new HelloService();
    }
}

SpringBoot中@PropertySource和@ImportResource注解的使用_第5张图片

配置文件占位符

1.随机数

person.last-name=韩剧小王子${random.uuid}
person.age=${random.int}

占位符获取之前配置的值,如果没有可以使用:指定默认值

person.last-name=韩剧小王子${random.uuid}
person.age=${random.int}
person.birth=2018/2/19
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,nan
person.dog.name=${person.last-name}_dog
person.dog=.age=2

SpringBoot中@PropertySource和@ImportResource注解的使用_第6张图片
SpringBoot中@PropertySource和@ImportResource注解的使用_第7张图片
当没有person.hello是使用默认值xzz

你可能感兴趣的:(SpringBoot中@PropertySource和@ImportResource注解的使用)