关于@PropertySource配置的用法解析

 更新时间:2022年03月25日 11:16:41   作者:马尔斯的蓝色  
这篇文章主要介绍了关于@PropertySource配置的用法解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

@PropertySource配置用法

功能

加载指定的属性文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value 和@ConfigurationProperties 使用。

@PropertySource 和 @Value组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中。

@PropertySource 和 @ConfigurationProperties组合使用,可以将属性文件与一个Java类绑定,将属性文件中的变量值注入到该Java类的成员变量中。

源码

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

使用示例

属性文件:demo.properties

demo.name=huang
demo.sex=1
demo.type=demo

示例一:@PropertySource + @Value

package com.huang.pims.demo.props;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"demo/props/demo.properties"})
public class ReadByPropertySourceAndValue {
    @Value("${demo.name}")
    private String name;
    @Value("${demo.sex}")
    private int sex;
    @Value("${demo.type}")
    private String type;
    @Override
    public String toString() {
        return "ReadByPropertySourceAndValue{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", type='" + type + '\'' +
                '}';
    }
}

示例二:@PropertySource 和 @ConfigurationProperties

package com.huang.pims.demo.props;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"demo/props/demo.properties"})
@ConfigurationProperties(prefix = "demo")
public class ReadByPropertySourceAndConfProperties {
    private String name;
    private int sex;
    private String type;
    public void setName(String name) {
        this.name = name;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getName() {
        return name;
    }
    public int getSex() {
        return sex;
    }
    public String getType() {
        return type;
    }
    @Override
    public String toString() {
        return "ReadByPropertySourceAndConfProperties{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", type='" + type + '\'' +
                '}';
    }
}

示例测试

package com.huang.pims.demo.runners;
import com.huang.pims.demo.props.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class OutputPropsRunner implements CommandLineRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class);
    @Autowired
    private ReadByPropertySourceAndValue readByPropertySourceAndValue;
    @Autowired
    private ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties;
    @Override
    public void run(String... args) throws Exception {
        LOGGER.info(readByPropertySourceAndValue.toString());
        LOGGER.info(readByPropertySourceAndConfProperties.toString());
    }
}

启动项目即可看到效果。 

从截图中可以看出,需要读取的属性配置,都已经成功读取出来了。

@PropertySource注解

@PropertySource是Spring boot为了方便引入properties配置文件提供的一个注解,可以标注在SpringBoot的启动类上,还可以标注在配置类(使用@Configuration标注的类)上。

例如

@PropertySource(value = {"classpath:box.properties"})

将classpath下的box.properties,注入到Spring环境中,使用@Value("${key}")取值。

示例

box.properties文件:

# 工具箱配置 
preserveFilePath=/box/webserver/uploadfile/preservefile/

注入:

@SpringBootApplication
@PropertySource(value = {"classpath:box.properties"})
public class ToolboxApiApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ToolboxApiApplication.class, args);
    } 
}

取值: 

@RestController
public class DeleteFileController {
    @Value("${preserveFilePath}")
    private String preserveFilePath;
 
    @GetMapping("/deleteFile")
    public void test(){
        System.out.println(preserveFilePath);
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • Eclipse将Maven项目打成jar包的方法

    Eclipse将Maven项目打成jar包的方法

    这篇文章主要介绍了Eclipse将Maven项目打成jar包的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2007-09-09
  • Java中和队列相关的基本操作

    Java中和队列相关的基本操作

    在Java中,队列是一种常用的数据结构,用于存储和管理元素。Java提供了Queue接口和其实现类,包括LinkedList和ArrayDeque等。队列的基本操作包括入队(enqueue)、出队(dequeue)、获取队首元素(peek)和判断队列是否为空(isEmpty)。
    2023-09-09
  • Java并发系列之ConcurrentHashMap源码分析

    Java并发系列之ConcurrentHashMap源码分析

    这篇文章主要为大家详细分析了Java并发系列之ConcurrentHashMap源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Mybatis中实体类属性与数据列表间映射方法介绍

    Mybatis中实体类属性与数据列表间映射方法介绍

    这篇文章主要介绍了Mybatis中实体类属性与数据列表间映射方法介绍,一共四中方法,供大家参考。
    2017-10-10
  • java序列化与反序列化操作实例分析

    java序列化与反序列化操作实例分析

    这篇文章主要介绍了java序列化与反序列化操作,结合实例形式分析了java序列化与反序列化的概念与具体实现技巧,需要的朋友可以参考下
    2016-10-10
  • Spring security如何实现记录用户登录时间功能

    Spring security如何实现记录用户登录时间功能

    这篇文章主要介绍了Spring security如何实现记录用户登录时间功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • java实现停车场系统

    java实现停车场系统

    这篇文章主要为大家详细介绍了java实现停车场系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • Spring mvc JSON数据交换格式原理解析

    Spring mvc JSON数据交换格式原理解析

    这篇文章主要介绍了Spring mvc JSON数据交换格式原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 关于Spring中一级缓存、二级缓存和三级缓存的那些事

    关于Spring中一级缓存、二级缓存和三级缓存的那些事

    Spring解决循环依赖的核心思想在于提前曝,下面这篇文章主要给大家介绍了关于Spring中一级缓存、二级缓存和三级缓存的那些事,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-02-02
  • java实现将字符串中首字母转换成大写,其它全部转换成小写的方法示例

    java实现将字符串中首字母转换成大写,其它全部转换成小写的方法示例

    这篇文章主要介绍了java实现将字符串中首字母转换成大写,其它全部转换成小写的方法,涉及java字符串遍历、转换、拼接等相关操作技巧,需要的朋友可以参考下
    2019-06-06

最新评论