日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

SpringBoot讀取配置文件常用方法解析

瀏覽:13日期:2023-05-05 16:04:13

首先回憶一下在沒有使用SpringBoot之前也就是傳統的spring項目中是如何讀取配置文件,通過I/O流讀取指定路徑的配置文件,然后再去獲取指定的配置信息。

傳統項目讀取配置方式

讀取xml配置文件

public String readFromXml(String xmlPath, String property) { SAXReader reader = new SAXReader(); Document doc = null; try { doc = reader.read(new File(xmlPath)); } catch (DocumentException e) { e.printStackTrace(); } Iterator<Element> iterator = doc.getRootElement().elementIterator(); while (iterator.hasNext()){ Element element = iterator.next(); if (element.getQName().getName().equals(property)){ return element.getTextTrim(); } } return null; }

讀取.properties配置文件

public String readFromProperty(String filePath, String property) { Properties prop = new Properties(); try { prop.load(new FileInputStream(filePath)); String value = prop.getProperty(property); if (value != null) { return value; } } catch (IOException e) { e.printStackTrace(); } return null; }

SpringBoot讀取配置方式

如何使用SpringBoot讀取配置文件,從使用Spring慢慢演變,但是本質原理是一樣的,只是SpringBoot簡化配置,通過注解簡化開發,接下來介紹一些常用注解。

@ImportResource注解

這個注解用來導入Spring的配置文件,是配置文件中的內容注入到配置類中,參數是一個數組,可以注入多個配置文件

代碼演示:

在SpringBoot項目的resources目錄下創建一個xml配置文件beans.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' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'> <bean class='com.example.test.config.ConfigBean'> <property name='dbType' value='Oracle'/> <property name='driverClassName' value='jdbc.driver.Oracle.OracleDriver'/> <property name='host' value='127.0.0.1'/> <property name='userName' value='oracle'/> <property name='password' value='oracle'/> </bean></beans>

創建配置類ConfigBean

package com.example.test.config;import lombok.Getter;import lombok.Setter;import lombok.ToString;/** * @author Vincente * @date 2020/07/12-12:29 * @desc 配置類 **/@Setter@Getter@ToStringpublic class ConfigBean { private String dbType; private String driverClassName; private String host; private String userName; private String password;}

添加@ImportResource注解,在SpringBoot項目的啟動類添加

package com.example.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportResource;@SpringBootApplication@ImportResource(locations = {'classpath:beans.xml'})public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); }}

測試代碼

package com.example.test;import com.example.test.config.ConfigBean;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest@RunWith(SpringRunner.class)class TestApplicationTests { @Autowired private ConfigBean configBean; @Test void testConfigBean(){ System.out.println(configBean); }}

輸出結果

ConfigBean(dbType=Oracle, driverClassName=jdbc.driver.Oracle.OracleDriver, host=127.0.0.1, userName=oracle, password=oracle)

小結 @ImportResource注解可以用來加載一個外部xml文件,注入到項目完成配置,但是這樣引入xml并沒有達到SpringBoot簡化配置的目的。

@Configuration和@Bean注解#

@Configuration和@Bean注解并不能讀取配置文件中的信息,但是這兩個類本身用來定義配置類

@Configuration用來代替xml文件,添加在一個類上面

@Bean用來代替bean標簽,聲明在方法上,方法的返回值返回一個對象到Spring的IoC容器中,方法名稱相當于bean標簽中的ID

代碼樣例

聲明一個bean

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author Vincente * @date 2020/07/12-13:28 * @desc **/@Configurationpublic class RestTemplateConfig { @Bean public RestTemplateConfig restTemplate(){ return new RestTemplate(); }}

測試代碼

package com.example.test;import com.example.test.config.RestTemplateConfig;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@SpringBootTest@RunWith(SpringRunner.class)class TestApplicationTests { @Resource private RestTemplateConfig restTemplate; @Test void testConfigBean(){ System.out.println(restTemplate); }}

輸出結果

com.example.test.config.RestTemplateConfig@de7e193

@Import注解

@Import注解是用來導入配置類或者一些需要前置加載的類,帶有@Configuration的配置類(4.2 版本之前只可以導入配置類,4.2版本之后 也可以導入 普通類)

代碼樣例

結合上面的代碼做修改,不全部貼出

將RestTemplateConfigestTemplateConfig類中的@Configuration注解去掉,在ConfigBean中導入

@Setter@Getter@ToString@Import(RestTemplateConfig.class)public class ConfigBean { private String dbType; private String driverClassName; private String host; private String userName; private String password;}

測試代碼

package com.example.test;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@SpringBootTest@RunWith(SpringRunner.class)class TestApplicationTests { @Resource ApplicationContext ctx; @Test void testConfigBean(){ System.out.println(ctx.getBean('restTemplate')); }}

輸出結果

com.example.test.config.RestTemplateConfig@6cd15072

小結 可以看到在IoC容器中已經導入了RestTemplateConfig(普通)類,這個注解類似于之前applicationContext.xml中的import標簽

@ConfigurationProperties和@Value#

@ConfigurationProperties和@Value這兩個注解算是在SpringBoot中用的比較多的注解了,可以在項目的配置文件application.yml和application.properties中直接讀取配置,但是在用法上二者也是有一定的區別

代碼樣例

創建配置文件application.yml

db-config: db-type: Oracle driver-class-name: jdbc.driver.Ooracle.OracleDriver host: 127.0.0.1 user-name: Oracle password: Oracleserver: port: 8080

創建配置類ConfigBean

package com.example.test.config;import lombok.Getter;import lombok.Setter;import lombok.ToString;import org.springframework.boot.context.properties.ConfigurationProperties;/** * @author Vincente * @date 2020/07/12-12:29 * @desc 配置類 **/@Setter@Getter@ToString@ConfigurationProperties(prefix = 'db-config')public class ConfigBean { private String dbType; private String driverClassName; private String host; private String userName; private String password;}

測試代碼

package com.example.test;import com.example.test.config.ConfigBean;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@SpringBootTest@RunWith(SpringRunner.class)class TestApplicationTests { @Resource ConfigBean configBean; @Value('${server.port}') private String port; @Test void testConfigBean(){ System.out.println(configBean); System.out.println(port); }}

輸出結果

ConfigBean(dbType=Oracle, driverClassName=jdbc.driver.Ooracle.OracleDriver, host=127.0.0.1, userName=Oracle, password=Oracle)8080

-總結 二者的一些區別

特性 @ConfigurationProperties @Value SpEL表達式 不支持 支持 屬性松散綁定 支持 不支持 JSR303數據校驗 支持 不支持

添加校驗注解

package com.example.test.config;import lombok.Getter;import lombok.Setter;import lombok.ToString;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.validation.annotation.Validated;import javax.validation.constraints.Null;/** * @author Vincente * @date 2020/07/12-12:29 * @desc 配置類 **/@Setter@Getter@ToString@ConfigurationProperties(prefix = 'db-config')@Validatedpublic class ConfigBean { @Null private String dbType; private String driverClassName; private String host; private String userName; private String password;}

輸出結果

Description:Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under ’db-config’ to com.example.test.config.ConfigBean failed: Property: db-config.dbType Value: Oracle Origin: class path resource [application.yml]:2:12 Reason: 必須為null

@PropertySource注解

@ConfigurationProperties和@Value這兩個注解默認從項目的主配置文件中讀取配置,當項目配置較多全部從一個地方讀取會顯得臃腫,可以將配置文件按照模塊拆分讀取到不同的配置類中,可以使用@PropertySource配合@Value讀取其他配置文件

代碼樣例

創建配置文件db-config.yml

/** * @author Vincente * @date 2020/07/12-14:19 * @desc **/db-config: db-type: Oracle driver-class-name: jdbc.driver.Ooracle.OracleDriver host: 127.0.0.1 user-name: Oracle password: Oracle

創建配置類ConfigBean

package com.example.test.config;import lombok.Getter;import lombok.Setter;import lombok.ToString;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * @author Vincente * @date 2020/07/12-12:29 * @desc 配置類 **/@Setter@Getter@ToString@PropertySource('classpath:db-config.yml')@Componentpublic class ConfigBean { @Value('${db-type}') private String dbType; @Value('${driver-class-name}') private String driverClassName; @Value('${host}') private String host; @Value('${user-name}') private String userName; @Value('${password}') private String password;}

測試代碼

package com.example.test;import com.example.test.config.ConfigBean;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@SpringBootTest@RunWith(SpringRunner.class)class TestApplicationTests { @Resource ConfigBean configBean; @Test void testConfigBean(){ System.out.println(configBean); }}

輸出結果

ConfigBean(dbType=Oracle, driverClassName=jdbc.driver.Ooracle.OracleDriver, host=127.0.0.1, userName=Vincente, password=Oracle)

小結

@PropertySource 用于獲取類路徑下的db-config.yml配置文件,@Value用于獲取yml中的配置信息,@Component注解用來將配置類交給Spring容器管理

總結

SpringBoot中提供了注解代替配置文件的方式來獲取項目中的配置,大大簡化了開發,以上總結了常用的讀取配置的方法,簡單來說就是兩種文件(yml和properties)幾大注解(@Value,@PropertySource,@Configuration,@ConfigurationProperties,@Import,@Bean);首先要了解每個注解的使用場景后,其次根據項目實際情況來具體的使用

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩国产在线观看| 欧美一区在线观看视频| 欧美日韩一区二区三区四区在线观看| 久久精品99久久无色码中文字幕| 久久一区精品| 六月婷婷一区| 欧美一区二区性| 久久久久久黄| 成人免费网站www网站高清| 精品女同一区二区三区在线观看| 影音先锋久久精品| 中文日韩欧美| 亚州av乱码久久精品蜜桃| 天堂网av成人| 91精品蜜臀一区二区三区在线| 成人亚洲一区| 成人片免费看| av资源中文在线| 亚洲天堂一区二区| 99视频精品全国免费| 欧产日产国产精品视频| 久久只有精品| 日韩精品永久网址| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 中文久久精品| 美女日韩在线中文字幕| 欧美精品一线| 中文字幕亚洲精品乱码| 欧美日韩一区二区三区四区在线观看| 亚洲精一区二区三区| 久久亚洲国产| 婷婷精品在线| 国产麻豆一区二区三区精品视频| 日本午夜精品视频在线观看| 97久久精品| 免费日韩成人| 日韩国产欧美| 精品一区毛片| 欧美专区18| 亚洲精品一二三**| 国产精品一页| 岛国精品一区| 久久精品欧美一区| 香蕉国产精品| 免费在线成人网| 日本成人一区二区| 国产精品最新| 色婷婷色综合| 香蕉国产精品| 亚洲五月综合| 欧美精品99| 欧美在线观看视频一区| 蜜桃视频一区二区| 久久99久久久精品欧美| 精品亚洲美女网站| 最新亚洲激情| 91av一区| 国产高潮在线| 亚洲欧洲一区二区天堂久久| 日本不卡中文字幕| 精品女同一区二区三区在线观看| 91精品国产成人观看| 蜜桃久久久久久久| 欧美精品成人| 国产+成+人+亚洲欧洲在线| 在线日韩中文| 欧美日本不卡高清| 亚洲欧洲午夜| 麻豆一区二区99久久久久| 欧美理论视频| 欧美久久久网站| 久久香蕉国产| 国产一区二区三区久久久久久久久| 日韩精品一卡| 欧美一区免费| 伊人久久亚洲热| 国产福利电影在线播放| 久久国产精品免费精品3p| 亚洲二区三区不卡| 国产精品videossex| 久久国产精品亚洲77777| 亚洲成av在线| 美女性感视频久久| 欧美一区=区三区| 天堂va蜜桃一区二区三区| 久久电影tv| 久久永久免费| 国产日本亚洲| 蜜臀久久久99精品久久久久久| 色综合www| 国产 日韩 欧美 综合 一区| 欧美午夜网站| 日韩一区二区三区免费视频| 在线一区视频| 999久久久免费精品国产| 狂野欧美性猛交xxxx| 日本电影久久久| 中文字幕一区日韩精品| 亚洲一区观看| 亚洲经典在线| 亚洲二区免费| 亚洲va中文在线播放免费| 久久男人av| 国产激情综合| 国产精品视频一区二区三区四蜜臂 | 色婷婷色综合| 你懂的亚洲视频| 国产免费播放一区二区| 日本三级亚洲精品| 日韩成人午夜精品| 日本va欧美va瓶| 日韩精选在线| 日韩高清一区在线| 日本精品另类| 久久国产生活片100| 日本精品久久| 欧美视频久久| 国产欧美日韩精品一区二区免费 | 久久免费精品| 精品美女久久| 97在线精品| 中文字幕成在线观看| 毛片在线网站| 久久国产电影| 亚洲专区一区| 美国三级日本三级久久99 | 99国产一区| 美女尤物久久精品| 中文字幕中文字幕精品| 精品一区二区三区亚洲| 中文字幕在线官网| 亚洲香蕉久久| 在线国产一区二区| 欧美天堂一区| 欧美中文高清| 国产情侣一区| 久久天堂影院| 91嫩草亚洲精品| 日韩视频网站在线观看| 福利一区二区三区视频在线观看| 日韩国产一区二区三区| 欧美影院三区| 视频一区视频二区中文| 亚洲精品日韩久久| 久久国产精品免费一区二区三区 | 欧美永久精品| 国产成人精选| 不卡视频在线| 亚久久调教视频| 88久久精品| 福利一区二区免费视频| 精品一区三区| 亚洲精品影视| 美女久久久久久 | 亚洲精品88| 亚洲电影在线一区二区三区| 麻豆久久一区| 99久久亚洲精品蜜臀| 亚洲综合欧美| 国产精品白浆| 99热国内精品| 国产亚洲精品v| 国产日产精品_国产精品毛片| 国产欧洲在线| 蜜臀久久久久久久| 精品国产aⅴ| 国产精品老牛| 国产精品17p| 欧美日韩激情在线一区二区三区| 色狠狠一区二区三区| 精品美女在线视频| 日韩一级不卡| 国产精品毛片视频| 欧美~级网站不卡| 国产日韩免费| 精品欧美激情在线观看| 日韩国产精品久久久| 精品国模一区二区三区| 亚洲资源在线| 国产欧洲在线| 蜜桃免费网站一区二区三区| 精品午夜视频| 免费在线观看视频一区| 欧美国产小视频| 免费在线成人网| av中文资源在线资源免费观看| 老司机久久99久久精品播放免费| 精品国产中文字幕第一页| 中文欧美日韩| 久久精品人人| 亚洲色图综合| 福利在线免费视频| 日韩一区二区三区免费视频| 日韩三区免费| 国产探花一区| 午夜欧美理论片| 欧美丰满日韩| 欧美日韩午夜电影网| 最新亚洲激情| 欧美片第1页|