spring配置加载顺序,spring的加载方式

  spring配置加载顺序,spring的加载方式

  1 .春天加载阳明海运股份有限公司文件2 .春天加载性能文件3 .春天加载系统磁盘(属性)文件4 .春天加载可扩展标记语言文件5.爪哇基于输入流读取性能配置文件本文默认弹簧版本是春天5

  一个弹簧加载阳明海运股份有限公司文件

  2春天加载性能文件

  3春天加载系统磁盘文件

  四春天加载可扩展标记语言文件

  5 Java基于输入流读取性能配置文件

  弹簧框架默认加载配置:

  资源下的文件名称为应用的应用程序。阳明海运股份有限公司以及应用程序。属性,默认会被弹簧加载到容器容器

  中,如果他们有重复的配置项,会被默认合并,并且应用程序。属性优先级更高。下面的LoadYmlTest.java有详细解释

  示例:

  #应用程序。ymluser :用户名: ifredom _ name年龄: 30 #定义了属性年龄

  #应用程序。属性#也定义了属性ageuser.age=99user.sex=2

  包com。举例。commonmybatisplus。配置;进口龙目岛。数据;导入org。spring框架。靴子。语境。属性。配置属性;导入org。spring框架。语境。注释。配置;导入org。spring框架。语境。注释。财产来源;/** * 配置类*春天默认规则:自动将资源下名称为应用的阳明海运股份有限公司和性能文件加载为bean *注意:一定要有得到和设置方法,此处通过@数据注解注入*/@数据@配置@配置属性(前缀=用户)公共类LoadPropertySourceYmlConfig {私有字符串用户名;私人(同Internationalorganizations)国际组织性别;私人年龄;}包com。举例。commonmybatisplus。财产来源测试;导入com。举例。commonmybatisplus。配置。loadpropertysourceymlconfig导入com。举例。commonmybatisplus。实体。用户实体;导入org。朱尼特。木星。API。测试;导入org。spring框架。豆子。工厂。注释。自动连线;导入org。spring框架。靴子。测试。语境。弹簧靴测试;导入org。spring框架。核心。环境。环境;/** * 测试* 从结果可知:年龄的值为99,证明了性能的优先级高于yml *事实上,春天最早支持性能类型文件,后来才支持的yml,所以为了兼容* 一定是属性阳明海运股份有限公司优先级。 * 即便将来又出现了XXX.abc文件* 那么为了兼容,永远时最早支持的优先级更高,也就是properties yml ABC */@ spring boot test公共类LoadYmlTest { @ auto wired private LoadPropertySourceYmlConfig ymlConfig;@ Test public void testYml(){ String name=yml config。获取用户名();int sex=yml配置。getsex();int age=yml配置。getage();System.out。普里

  ntln(name); // ifredom System.out.println(sex); // 2 System.out.println(age); // 99 }}

 

  

1.spring加载yml文件

上面已经通过默认的方式演示了如何加载默认的application.yml,但是更好的写法是,将我们自定义的配置独立为一个单独的文件。

 

  比如我们开发微信公众号,小程序开发时,就需要用到配置,此时的配置就应该独立为一个额外的文件。(此处将演示加载为 List)

  一共有3步:

  创建配置文件 wechat-config.yml定义配置类 LoadPropertySourceYmlConfig,用来加载yml中的数据 (指定配置类的加载器) (因为 spring 配置类的默认加载器是 PropertiesLoader加载器,所以我们需要自定义 yml加载器。可以自行查看注解 @PropertySource 源码)自定义配置类加载器 YamlSourceFactory,继承Spring提供的默认配置类构造器 DefaultPropertySourceFactory测试示例:

  

# main/resources/chat-config.yml

 

  #微信小程序的appidappid: app-xxx#微信小程序的Secretsecret: secretxxx#微信小程序消息服务器配置的tokentoken: token-xxx#微信小程序消息服务器配置的EncodingAESKeyaesKey: aesKey-xxx

  wx: configs: #微信小程序的appid - appid: app1 #微信小程序的Secret secret: secret1 #微信小程序消息服务器配置的token token: token1 #微信小程序消息服务器配置的EncodingAESKey aesKey: aesKey1

   #微信小程序的appid - appid: appid2 #微信小程序的Secret secret: secret2 #微信小程序消息服务器配置的token token: token2 #微信小程序消息服务器配置的EncodingAESKey aesKey: aesKey2

  def-my-var1: 定义配置属性var1def-my-var2: 定义配置属性var2

  

这里需要注意,在上面的配置文件中是 wx.configs, 因此在配置类中,也必须用同名的 configs 来接收

 

  

package com.example.commonmybatisplus.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.io.support.PropertySourceFactory;import org.springframework.stereotype.Component;import java.util.List;/** * 注意:factory的值,为接下来自定义的加载器 */@Data@Configuration@PropertySource(value = "classpath:wechat-config.yml", factory = YamlSourceFactory.class)@ConfigurationProperties(prefix = "wx")public class LoadPropertySourceYmlConfig { private List<Config> configs; @Data public static class Config { private String appid; private String secret; private String token; private String aesKey; } private String appid; private String secret; private String token; private String aesKey;}
// 自定义记载器package com.example.commonmybatisplus.config;import org.springframework.boot.env.YamlPropertySourceLoader;import org.springframework.core.env.PropertySource;import org.springframework.core.io.support.DefaultPropertySourceFactory;import org.springframework.core.io.support.EncodedResource;import java.io.IOException;import java.util.List;public class YamlSourceFactory extends DefaultPropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { // 这里使用Yaml配置加载类来读取yml文件信息 List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource()); return sources.get(0); }}

测试结果

 

  

package com.example.commonmybatisplus.PropertySourceTest;import com.example.commonmybatisplus.config.LoadPropertySourceYmlConfig;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;/** * @Author ifredomvip@gmail.com * @Date 2022/6/15 10:40 * @Version 1.0.0 * @Description **/@SpringBootTestpublic class LoadYmlTest { @Autowired private LoadPropertySourceYmlConfig ymlConfig; @Test public void testYml() { String appidXXX = ymlConfig.getAppid(); String secretXXX = ymlConfig.getSecret(); System.out.println("单独的属性配置---appidXXX: " + appidXXX); System.out.println("单独的属性配置---secretXXX: " + secretXXX); // 以下演示 配置项作为 List List<LoadPropertySourceYmlConfig.Config> configs = ymlConfig.getConfigs(); // 迭代 List 每一项 for (LoadPropertySourceYmlConfig.Config config : configs) { System.out.println("属性作为List: " + config); } // 获取List种的某一项 LoadPropertySourceYmlConfig.Config configFirst = configs.get(0); String appidFirst = configFirst.getAppid(); System.out.println("List的第一项: " + configFirst); System.out.println("List的第一项的其中一个属性: " + appidFirst); LoadPropertySourceYmlConfig.Config configSecond = configs.get(1); String secretSecond = configSecond.getSecret(); System.out.println("List的第二项: " + configSecond); System.out.println("List的第二项的其中一个属性: " + secretSecond); }}

 

  

 

  

2.spring 加载 properties 文件

从上一步我们已经知道了, spring 默认使用 properties 文件的加载器。因此,我们可以少一步构造加载器

 

  创建配置文件 alibaba-config.properties定义配置类 LoadPropertySourceConfig,用来加载yml中的数据测试

# main/resources/alibaba-config.propertiesali-yun.username="ifredom"ali-yun.password="123456"ali-yun.blog="http://www.ifredom.com"

 

  

package com.example.commonmybatisplus.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;@Data@Configuration@PropertySource("classpath:alibaba-config.properties")@ConfigurationProperties(prefix = "aliyun")public class LoadPropertySourceConfig { private String username; private String password; private String blog;}

测试:

 

  

package com.example.commonmybatisplus.PropertySourceTest;import com.example.commonmybatisplus.config.LoadPropertySourceConfig;import com.example.commonmybatisplus.config.LoadPropertySourceYmlConfig;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;/** * @Author ifredomvip@gmail.com * @Date 2022/6/15 10:40 * @Version 1.0.0 * @Description **/@SpringBootTestpublic class LoadYmlTest { @Autowired private LoadPropertySourceYmlConfig ymlConfig; @Autowired private LoadPropertySourceConfig propertyConfig; @Test public void testProperty() { String username = propertyConfig.getUsername(); String password = propertyConfig.getPassword(); String blog = propertyConfig.getBlog(); System.out.println("单独的属性配置---username: " + username); System.out.println("单独的属性配置---password: " + password); System.out.println("单独的属性配置---blog: " + blog); } @Test public void testYml() { }}

细心的同学应该发现了,在配置文件中定义的是ali-yun中间明明有一个短横线,读取属性的时候怎么没了?

 

  这是因为 Spring 配置加载器类对诸如 空格,下划线,短横线,大小写都做了替空处理,也就是:配置文件是不分大小写的; 并且 下划线,短横线也会忽略: user_name -> username, pass-word -> password.因此取名你可以很随意

  

 

  

3.spring加载系统磁盘(properties)文件

有时候我们会需要加载其他项目下的数据库,而配置文件并不在当前项目路劲下,因此需要指定文件路径。

 

  磁盘路径可以是相对路径,绝对路径,也可以通过系统属性值指定变量相对路径,文件在应用根目录下:@PropertySource(value = {"file:project1.properties"})相对路径,文件在应用根目录下:@PropertySource(value = {"file:./project1.properties"})绝对路径,在指定的路径下:@PropertySource(value = {"file:D:\project\project1.properties"})通过系统属性值指定变量:@PropertySource(value = {"file:${user.dir}/project1.properties"})由于加载xml文件还需要对xml文件进行解析,此处不做讲解。仅仅使用 properties 文件做例子。 示例:

  

# 位于D盘下的配置文件 D:project1.propertiesdriverClassName=com.mysql.cj.jdbc.Driverurl="https://www.ifredom.com"username="ifredom"password="123456"

 

  

由于配置文件没有前缀,因此 配置类 必须使用@Value()进行映射

 

  

package com.example.commonmybatisplus.config;import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;@Data@Configuration@PropertySource(value = {"file:D:\project1.properties"})public class LoadDiskConfig { @Value("driverClassName") private String driverClassName; @Value("url") private String url; @Value("username") private String username; @Value("password") private String password;}
/** * 测试 */@SpringBootTestpublic class LoadYmlTest { @Autowired private LoadDiskConfig diskConfig; @Test public void testDisk() { String username = diskConfig.getUsername(); String url = diskConfig.getUrl(); System.out.println(username); System.out.println(url); }}

 

  

4.spring加载xml文件

创建一个xml文件:applicationContext.xml,并在其中定义一个bean通过 ApplicationContext 来加载读取xml文件不再推介使用 xml 来读取文件了,过于复杂

 

  示例:

  

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <bean id="blog" name="author" class="com.example.commonmybatisplus.entity.UserEntity"> <property name="id" value="1"/> <property name="name" value="ifredom"/> <property name="age" value="30"/> </bean></beans>
@Datapublic class UserEntity { private Long id; private String name; private int sex; private int age;}
@SpringBootTestpublic class LoadYmlTest { @Test public void testXml() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserEntity author = context.getBean("author", UserEntity.class); System.out.println(author.getName()); }}

 

  

5.Java基于InputStream读取properties配置文件

import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.List;import java.util.Properties;@SpringBootTestpublic class LoadYmlTest { @Test public void testInputStream() throws IOException { Properties properties = new Properties(); // 使用InPutStream流读取properties文件 BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/project1.properties")); properties.load(bufferedReader); // 获取key对应的value值 String driverClassName = properties.getProperty("driverClassName"); String username = properties.getProperty("username"); System.out.println(driverClassName); System.out.println(username); }}

到此这篇关于Spring详解四种加载配置项的方法的文章就介绍到这了,更多相关Spring加载配置项内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • spring编程式事务处理,spring编程事务
  • spring编程式事务处理,spring编程事务,详解Spring学习之编程式事务管理
  • spring的核心功能模块有几个,列举一些重要的spring模块
  • spring的核心功能模块有几个,列举一些重要的spring模块,七个Spring核心模块详解
  • spring注解和springmvc的注解,SpringMVC常用注解
  • spring注解和springmvc的注解,SpringMVC常用注解,详解springmvc常用5种注解
  • spring实现ioc的四种方法,spring的ioc的三种实现方式
  • spring实现ioc的四种方法,spring的ioc的三种实现方式,简单实现Spring的IOC原理详解
  • spring事务失效问题分析及解决方案怎么做,spring 事务失效情况
  • spring事务失效问题分析及解决方案怎么做,spring 事务失效情况,Spring事务失效问题分析及解决方案
  • spring5.0新特性,spring4新特性
  • spring5.0新特性,spring4新特性,spring5新特性全面介绍
  • spring ioc以及aop原理,springmvc aop原理
  • spring ioc以及aop原理,springmvc aop原理,深入浅析Spring 的aop实现原理
  • Spring cloud网关,spring cloud zuul作用
  • 留言与评论(共有 条评论)
       
    验证码: