Spring中@PropertySource配置的用法

 更新时间:2023年11月23日 09:12:37   作者:马尔斯的蓝色  
这篇文章主要介绍了Spring中@PropertySource配置的用法,@PropertySource 和 @Value
组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中,需要的朋友可以参考下

@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());
    }

}

启动项目即可看到效果。

在这里插入图片描述

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

到此这篇关于Spring中@PropertySource配置的用法的文章就介绍到这了,更多相关@PropertySource配置的用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中使用opencv的问题

    Java中使用opencv的问题

    这篇文章主要介绍了Java中使用opencv的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • 深入浅出重构Mybatis与Spring集成的SqlSessionFactoryBean(上)

    深入浅出重构Mybatis与Spring集成的SqlSessionFactoryBean(上)

    通常来讲,重构是指不改变功能的情况下优化代码,但本文所说的重构也包括了添加功能。这篇文章主要介绍了重构Mybatis与Spring集成的SqlSessionFactoryBean(上)的相关资料,需要的朋友可以参考下
    2016-11-11
  • Java 中 getClass() 方法的使用与原理深入分析(对象类型信息)

    Java 中 getClass() 方法的使用与原理深入分析(对象类型信息)

    在 Java 编程中,getClass() 是一个非常重要的方法,它用于获取对象的运行时类信息,无论是调试代码、反射操作,还是类型检查,getClass() 都扮演着关键角色,本文将深入探讨 getClass() 的使用方法、底层原理以及实际应用场景,感兴趣的朋友一起看看吧
    2024-12-12
  • 一文带你了解Spring中存入Bean和获取Bean的方式

    一文带你了解Spring中存入Bean和获取Bean的方式

    这篇文章主要带大家了解Spring中存入Bean和获取Bean的方式,文中的代码示例讲解的非常详细,对我们的学习或工作有一定的帮助,需要的朋友可以参考下
    2023-07-07
  • Mybatis-Plus环境配置与入门案例分析

    Mybatis-Plus环境配置与入门案例分析

    MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在,本篇文章带你配置环境并认识它
    2022-03-03
  • Java踩坑记录之BigDecimal类

    Java踩坑记录之BigDecimal类

    这篇文章主要给大家介绍了关于Java踩坑记录之BigDecimal类的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 浅谈MyBatis通用Mapper实现原理

    浅谈MyBatis通用Mapper实现原理

    这篇文章主要介绍了浅谈MyBatis通用Mapper实现原理,本文会先介绍通用 Mapper 的简单原理,然后使用最简单的代码来实现这个过程。感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Java中关于Collections集合工具类的详细介绍

    Java中关于Collections集合工具类的详细介绍

    Java提供了一个操作Set、List和Map等集合的工具类:Collections,该工具提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了将集合对象设置为不可变、对集合对象实现同步控制等方法
    2021-09-09
  • SpringBoot Bean花式注解方法示例下篇

    SpringBoot Bean花式注解方法示例下篇

    这篇文章主要介绍了SpringBoot Bean花式注解方法,很多时候我们需要根据不同的条件在容器中加载不同的Bean,或者根据不同的条件来选择是否在容器中加载某个Bean
    2023-02-02
  • java分割字符串多种方法(附例子)

    java分割字符串多种方法(附例子)

    这篇文章主要给大家介绍了关于java分割字符串多种方法的相关资料,Java中有多种方法可以实现字符串分割,文中将每张方法都给出了代码示例,需要的朋友可以参考下
    2023-10-10

最新评论