广告
返回顶部
首页 > 资讯 > 后端开发 > Python >关于@PropertySource配置的用法解析
  • 270
分享到

关于@PropertySource配置的用法解析

2024-04-02 19:04:59 270人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录@PropertySource配置用法功能源码使用示例示例测试@PropertySource注解例如示例@PropertySource配置用法 功能 加载指定的属性文件(*.pr

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。 

--结束END--

本文标题: 关于@PropertySource配置的用法解析

本文链接: https://www.lsjlt.com/news/143894.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作