使用@PropertySource加载配置文件

1.@PropertySource和@PropertySources注解

1.1.@PropertySource注解概述

@PropertySource注解是Spring 3.1开始引入的配置类注解。通过**@PropertySource注解可以将properties配置文件中的key/value存储到SpringEnvironment中,Environment接口提供了方法去读取配置文件中的值,参数是properties配置文件中定义的key值。当然了,也可以使用@Value注解用${}占位符**为bean的属性注入值。

@PropertySource注解的源代码,如下所示

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.io.support.PropertySourceFactory;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {

	String name() default "";

	String[] value();

	boolean ignoreResourceNotFound() default false;

	String encoding() default "";

	Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

从@PropertySource的源码中可以看出,可以通过@PropertySource注解指定多个properties文件,使用的形式如下所示:

@PropertySource(value={"classpath:/person.properties", "classpath:/car.properties"})

1.2.@PropertySources注解概述

@PropertySources注解的源码,如下所示:

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {

	PropertySource[] value();
}

@PropertySources注解的源码比较简单,只有一个PropertySource[]数组类型的value属性,那我们如何使用@PropertySources注解指定配置文件呢?其实也很简单,使用如下所示的方式就可以了。

@PropertySources(value={
    @PropertySource(value={"classpath:/person.properties"}),
    @PropertySource(value={"classpath:/car.properties"}),
})

2.@PropertySource注解的用法

2.1.使用注解方式获取值

在工程的src/main/resources目录下创建一个配置文件person.properties,内容如下所示:

person.nickName=liqb

在Person类中新增一个nickName字段,如下所示:

package com.tianxia.springannotation.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;

import java.io.Serializable;

/**
 * Person类
 * @author liqb
 * @date 2023-04-21 16:00
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person implements Serializable {

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 昵称
     */
    @Value("${person.nickName}")
    private String nickName;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

在MainConfigOfPropertyValues配置类上添加一个@PropertySource注解,如下所示:

package com.tianxia.springannotation.config;

import com.tianxia.springannotation.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 配置类
 * @author liqb
 * @date 2023-05-04 11:28
 **/
@Configuration
// 使用@PropertySource读取外部配置文件中的key/value保存到运行的环境变量中,加载完外部的配置文件以后,使用${}取出配置文件中的值
@PropertySource(value={"classpath:/person.properties"})
public class MainConfigOfPropertyValues {

    @Bean("propertySource_person")
    public Person person() {
        return new Person();
    }
}

此时Person类的nickName字段的值为空。运行测试方法,输出结果如下所示:

/**
 * 测试类
 * @author liqb
 * @date
 **/
@Test
public void test() {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
    Person person = applicationContext.getBean(Person.class);
    System.out.println(person);
}

在这里插入图片描述

此时Person类的nickName字段已经注入liqb这个值了

2.2.使用Environment获取值

使用@PropertySource注解读取外部配置文件中的key/value之后,是将其保存到运行的环境变量中了,所以我们也可以通过运行环境来获取外部配置文件中的值。

运行测试方法,输出结果如下所示:

/**
 * 使用Environment获取值
 * @author liqb
 * @date 2023-05-06 12:44
 */
@Test
public void test01() {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
    ConfigurableEnvironment environment = (ConfigurableEnvironment) applicationContext.getEnvironment();
    String property = environment.getProperty("person.nickName");
    System.out.println(property);
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值