iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >如何使用@PropertySource读取配置文件
  • 488
分享到

如何使用@PropertySource读取配置文件

2023-06-29 16:06:24 488人浏览 安东尼
摘要

本篇内容主要讲解“如何使用@PropertySource读取配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用@PropertySource读取配置文件”吧!@PropertySou

本篇内容主要讲解“如何使用@PropertySource读取配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用@PropertySource读取配置文件”吧!

    @PropertySource读取配置文件通过@Value参数注入

    有参数文件如下test.properties

    project.author=WPFcproject.create_time=2018/3/29

    在系统中读取对应的数据,并注入到属性中

    @Configuration@ComponentScan("cn.edu.ntu")@PropertySource("classpath:test.properties")public class ElConfig {     @Value("#{systemProperties['os.name']}")    private String osName;        //要想使用@Value 用${}占位符注入属性,这个bean是必须的(PropertySourcesPlaceholderConfigurer),    //这个就是占位bean,    //另一种方式是不用value直接用Envirment变量直接getProperty('key')      @Value("${project.author}")    public String author;        @Autowired    private Environment environment;        //You need this if you use @PropertySource + @Value    @Bean    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {       return new PropertySourcesPlaceholderConfigurer();    }        public void printProperties(){        System.out.println("os name : " + osName);        System.out.println("author  : " + author);        System.out.println("env     : " + environment.getProperty("project.create_time"));    }    }

    测试方法:

    public class MainApplication {    public static void main(String[] args){        AnnotationConfigApplicationContext context = null;        context = new AnnotationConfigApplicationContext(ElConfig.class);        ElConfig bean = context.getBean(ElConfig.class);        bean.printProperties();    }    }

    测试结果:

    os name : windows 7author  : wpfcenv     : 2018/3/29

    • @Import引入javaConfig配置的配置类

    • @ImportResource引入xml对应的配置文件

    spring读取配置@Value、@PropertySource、@ConfigurationProperties使用

    Spring (Boot)获取参数的方式有很多,其中最被我们熟知的为@Value了,它不可谓不强大。

    今天就针对我们平时最长使用的@Value,以及可能很少人使用的@PropertySource、@ConfigurationProperties等相关注解进行一个详细的扫盲,希望能够帮助到到家,使用起来更加顺畅

    @Value

    @Value注解的注入非常强大,可以借助配置文件的注入、也可以直接注入

    注入普通字符串

        @Value("nORMal")    private String normal; // normal (显然这种注入的意义不大)

    注入操作系统属性

    @Value("#{systemProperties['os.name']}")    private String systemPropertiesName; //效果等同于  是因为spring模版把系统变量否放进了Enviroment@Value("${os.name}")    private String systemPropertiesName;

    注入表达式结果

    @Value("#{ T(java.lang.Math).random() * 100.0 }")    private double randomNumber; //41.29185128620939

    注入其它Bean的属性:Person类的name属性

        @Bean    public Person person() {        Person person = new Person();        person.setName("fangshixiang");        return person;    } //注入属性    @Value("#{person.name}")    private String personName;     @Test    public void contextLoads() {        System.out.println(personName); //fangshixiang    }

    注入文件资源

    在resources下放置一个jdbc.properties配置文件。然后可以直接注入

        @Value("classpath:jdbc.properties")    private Resource resourceFile; // 注入文件资源     @Test    public void contextLoads() throws IOException {        System.out.println(resourceFile); //class path resource [jdbc.properties]        String s = FileUtils.readFileToString(resourceFile.getFile(), StandardCharsets.UTF_8);        System.out.println(s);        //输出:        //db.username=fangshixiang        //db.passWord=fang        //db.url=jdbc:mysql://localhost:3306/mytest        //db.driver-class-name=com.Mysql.jdbc.Driver    }

    注入Url资源

        @Value("Http://www.baidu.com")    private Resource testUrl; // 注入URL资源      @Test    public void contextLoads() {        System.out.println(testUrl); //URL [http://www.baidu.com]    }
    @Value中$和#的区别

    语法:

    ${ properties }和#{ SpEL }的语法区别

    ${ property : default_value }

    #{ obj.property? : default_value } 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量

    正常使用的情况,这里不做过多的介绍了,现在介绍一些异常情况

    ${ properties }`:这种比较简单,如果key找不到,启动会失败。如果找不到的时候也希望正常启动,可以采用冒号+默认值的方式

    #{ obj.property? : default_value }

        @Value("#{person}")    private Person value;     @Test    public void contextLoads() {        System.out.println(value); //Person(name=fangshixiang, age=null, addr=null, hobby=null)    }

    我们发现这个很强大,可以直接把容器的里的一个对象直接注入进来。只是我们可能一般不这么做。

    如果改成person1,在容器里找不到这个bean,也是会启动报错的。@Value("#{person1?:null}")这样也是不行的,因为person1找不到就会报错  

        @Value("#{person.name}")    private String personName;     @Value("#{person.age}")    private String perAge;     //注入默认值    @Value("#{person.age?:20}")    private String perAgeDefault;     //如果age22这个key根本就不存在,启动肯定会报错的    //@Value("#{person.age22?:20}")    //private String perAgeDefault22;     @Test    public void contextLoads() {        System.out.println(personName); //fangshixiang        System.out.println(perAge); //null        System.out.println(perAgeDefault); //20    }

    获取级联属性,下面两种方法都是ok的:

         @Value("#{person.parent.name}")    private String parentName1;     @Value("#{person['parent.name']}")    private String parentName2;     @Test    public void contextLoads() {        System.out.println(parentName1); //fangshixiang        System.out.println(parentName2); //fangshixiang    }

    二者结合使用:#{ ‘${}’ }

    注意结合使用的语法和单引号,不能倒过来。

    两者结合使用,可以利用SpEL的特性,写出一些较为复杂的表达式,如:

        @Value("#{'${os.name}' + '_' +  person.name}")    private String age;      @Test    public void contextLoads() {        System.out.println(age); //Windows 10_fangshixiang    }

    @PropertySource:加载配置属性源

    此注解也是非常非常的强大,用好了,可以很好的实现配置文件的分离关注,大大提高开发的效率,实现集中化管理

    最简单的应用,结合@Value注入属性值(也是最常见的应用)

    通过@PropertySource把配置文件加载进来,然后使用@Value获取

    @Configuration@PropertySource("classpath:jdbc.properties")public class PropertySourceConfig {     @Value("${db.url}")    private String dbUrl;     @PostConstruct    public void postConstruct() {        System.out.println(dbUrl); //jdbc:mysql://localhost:3306/mytest    }}

    @PropertySource各属性介绍

    • value数组。指定配置文件的位置。支持classpath:和file:等前缀 Spring发现是classpath开头的,因此最终使用的是Resource的子类ClassPathResource。如果是file开头的,则最终使用的类是FileSystemResource

    • ignoreResourceNotFound:默认值false。表示如果没有找到文件就报错,若改为true就不报错。建议保留false

    • encoding:加载进来的编码。一般不用设置,可以设置为UTF-8等等

    • factory:默认的值为DefaultPropertySourceFactory.class。

    @Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));}

    源码其实也没什么特别的。其重难点在于:

    DefaultPropertySourceFactory什么时候被Spring加载呢?

    name和resource都是什么时候被赋值进来的?

    本文抛出这两个问题,具体原因会在后续分析源码的相关文章中有所体现。

    需要注意的是PropertySourceFactory的加载时机早于Spring Beans容器,因此实现上不能依赖于Spring的ioc

    @PropertySource多环境配置以及表达式使用(spring.profiles.active)

    方法一:可以这么配置

    @PropertySource(“classpath:jdbc-${spring.profiles.active}.properties”)

    程序员在开发时不需要关心生产环境数据库的地址、账号等信息,一次构建即可在不同环境中运行

    @ConfigurationProperties

    注意:上面其实都是Spring Framwork提供的功能。而@ConfigurationProperties是Spring Boot提供的。包括@EnableConfigurationProperties也是Spring Boot才有的。它在自动化配置中起到了非常关键的作用

    ConfigurationPropertiesBindingPostProcessor会对标注@ConfigurationProperties注解的Bean进行属性值的配置。

    有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类

    该注解在Spring Boot的自动化配置中得到了大量的使用

    springMVC的自动化配置:

    @ConfigurationProperties(prefix = "spring.mvc")public class WEBMvcProperties {} //加载方式@Configuration@Conditional(DefaultDispatcherServletCondition.class)@ConditionalOnClass(ServletReGIStration.class)// 此处采用这个注解,可议把WebMvcProperties这个Bean加载到容器里面去~~~// WebMvcProperties里面使用了`@ConfigurationProperties(prefix = "spring.mvc")`@EnableConfigurationProperties(WebMvcProperties.class) //加载MVC的配置文件protected static class DispatcherServletConfiguration {}

    似乎我们能看出来一些该注解的使用方式。

    说明:这里说的两种,只是说的最常用的。其实只要能往容器注入Bean,都是一种方式,比如上面的@EnableConfigurationProperties方式也是ok的

    关于@EnableConfigurationProperties的解释,在注解驱动的Spring相关博文里会有体现

    加在类上,需要和@Component注解,结合使用.代码如下

    com.example.demo.name=${aaa:hi}com.example.demo.age=11com.example.demo.address[0]=北京  # 注意数组 List的表示方式 Map/Obj的方式各位可以自行尝试com.example.demo.address[1]=上海com.example.demo.address[2]=广州com.example.demo.phone.number=1111111

    java代码:

    @Component@ConfigurationProperties(prefix = "com.example.demo")public class People {     private String name;    private Integer age;    private List<String> address;    private Phone phone;}

    通过@Bean的方式进行声明,这里我们加在启动类即可,代码如下

       @Bean    @ConfigurationProperties(prefix = "com.example.demo")    public People people() {        return new People();    }

    此些方式并不需要使用@EnableConfigurationProperties去开启它。

    细节:Bean的字段必须有get/set方法,请注意~~~

    另外还有一种结合@PropertySource使用的方式,可谓完美搭配

    @Component@PropertySource("classpath:config/object.properties")@ConfigurationProperties(prefix = "obj")public class ObjectProperties {}

    其余属性见名之意,这里一笔带过:

    • ignoreInvalidFields

    • ignoreNestedProperties

    • ignoreUnknownFields

    如何使用@PropertySource读取配置文件

    简单理解:

    • @ConfigurationProperties 是将application配置文件的某类名下所有的属性值,自动封装到实体类中。

    • @Value 是将application配置文件中,所需要的某个属性值,封装到java代码中以供使用。

    应用场景不同:

    如果只是某个业务中需要获取配置文件中的某项值或者设置具体值,可以使用@Value;

    如果一个JavaBean中大量属性值要和配置文件进行映射,可以使用@ConfigurationProperties;

    到此,相信大家对“如何使用@PropertySource读取配置文件”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

    --结束END--

    本文标题: 如何使用@PropertySource读取配置文件

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

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

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

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

    下载Word文档
    猜你喜欢
    • 如何使用@PropertySource读取配置文件
      本篇内容主要讲解“如何使用@PropertySource读取配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用@PropertySource读取配置文件”吧!@PropertySou...
      99+
      2023-06-29
    • 使用@PropertySource读取配置文件通过@Value进行参数注入
      目录@PropertySource读取配置文件通过@Value参数注入Spring读取配置@Value、@PropertySource、@ConfigurationPropertie...
      99+
      2024-04-02
    • @PropertySource 无法读取配置文件的属性值解决方案
      原来Person类这样写: 只写了@PropertySource注解 @Component @PropertySource(value = {"classpath:person....
      99+
      2024-04-02
    • 如何读取Flex配置文件
      这篇文章主要介绍了如何读取Flex配置文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Flex在运行时如何读取Flex配置文件Flex简介AdobeFlex是为满足希望开发...
      99+
      2023-06-17
    • thinkphp5如何读取配置文件
      本篇内容介绍了“thinkphp5如何读取配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在使用ThinkPHP5框架时,我们通常需要...
      99+
      2023-07-05
    • java如何读取配置文件
      Java中可以使用java.util.Properties类来读取配置文件。Properties类提供了一些方法来读取和操作属性文件...
      99+
      2023-08-08
      java
    • python如何读取ini配置文件
      Python提供了一个标准库`configparser`用于读取和修改INI文件。首先,需要导入`configparser`模块:`...
      99+
      2023-10-08
      python
    • C#中如何读取配置文件
      C#中如何读取配置文件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。C#读取配置文件1、读取配置信息下面是一个配置文件的具体内容:   &nb...
      99+
      2023-06-18
    • .NetCore中如何读取配置文件
      小编给大家分享一下.NetCore中如何读取配置文件,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!在应用程序开发中,配置文件是主要存储系统的初始配置信息,配置文件的读取虽然属于基础内容却又经常用到,所以百丈高楼平地起,学习...
      99+
      2023-06-29
    • SpringBoot yml配置文件如何读取
      本篇内容主要讲解“SpringBoot yml配置文件如何读取”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot yml配置文件如何读取”吧!yaml介绍YA...
      99+
      2023-07-04
    • properties配置文件如何使用Spring Boot进行读取
      这篇文章给大家介绍properties配置文件如何使用Spring Boot进行读取,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。在SpringApplication类中: private ConfigurableE...
      99+
      2023-05-31
      springboot properties
    • SpringBoot如何读取外部配置文件
      这篇文章将为大家详细讲解有关SpringBoot如何读取外部配置文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.SpringBoot配置文件SpringBoot使用一个以application命名的...
      99+
      2023-06-29
    • 如何在C#中读取配置文件
      如何在C#中读取配置文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。C#读取配置文件1.了解配置文件概述:应 用程序配置文件是标准的 XML 文件,XML 标...
      99+
      2023-06-18
    • Java如何实现读取配置文件
      在Java中,可以使用`java.util.Properties`类来读取配置文件。以下是一个简单的例子:1. 创建一个名为`con...
      99+
      2023-09-28
      Java
    • @Scheduled 如何读取动态配置文件
      @Scheduled读取动态配置文件 application.yml配置文件得配置信息 agreeAccTask: # # 每3分钟执行一次,handTime: 0 0/3 *...
      99+
      2024-04-02
    • ASP.NETCore读取配置文件
      ASP.NET Core 中,可以使用 ConfigurationBuilder 对象来构建。 主要分为三部:配置数据源 -> ConfigurationBuilder -&g...
      99+
      2024-04-02
    • Unity 读取文件 TextAsset读取配置文件方式
      1 支持文件类型 .txt .html .htm .xml .bytes .json .csv .yaml .fnt 2 寻找文件 1 //Load texture from d...
      99+
      2024-04-02
    • Python读取配置文件
      文章目录 Python读取配置文件一、 yaml1、 准备2、 操作数据2.1 读取数据2.2 写入数据 二、 ini1、准备2、 操作数据2.1 读取数据2.2. 写数据 ...
      99+
      2023-09-03
      python 开发语言
    • redis读取配置文件
      redis 读取配置文件的路径位于 /etc/redis/redis.conf 或 /usr/local/etc/redis/redis.conf。它会读取配置文件并逐行解析,将配置选项...
      99+
      2024-04-19
      redis 键值对
    • .NETCore读取配置文件
      1. 说明 默认情况下读取配置Configuration的默认优先级:ConfigureAppConfiguration(自定义读取)>CommandLine(命令行参数)&g...
      99+
      2024-04-02
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作