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

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

傳統(tǒng)項目讀取配置方式

讀取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慢慢演變,但是本質(zhì)原理是一樣的,只是SpringBoot簡化配置,通過注解簡化開發(fā),接下來介紹一些常用注解。

@ImportResource注解

這個注解用來導(dǎo)入Spring的配置文件,是配置文件中的內(nèi)容注入到配置類中,參數(shù)是一個數(shù)組,可以注入多個配置文件

代碼演示:

在SpringBoot項目的resources目錄下創(chuàng)建一個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>

創(chuàng)建配置類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); }}

輸出結(jié)果

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

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

@Configuration和@Bean注解#

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

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

@Bean用來代替bean標(biāo)簽,聲明在方法上,方法的返回值返回一個對象到Spring的IoC容器中,方法名稱相當(dāng)于bean標(biāo)簽中的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); }}

輸出結(jié)果

com.example.test.config.RestTemplateConfig@de7e193

@Import注解

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

代碼樣例

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

將RestTemplateConfigestTemplateConfig類中的@Configuration注解去掉,在ConfigBean中導(dǎo)入

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

輸出結(jié)果

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

小結(jié) 可以看到在IoC容器中已經(jīng)導(dǎo)入了RestTemplateConfig(普通)類,這個注解類似于之前applicationContext.xml中的import標(biāo)簽

@ConfigurationProperties和@Value#

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

代碼樣例

創(chuàng)建配置文件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

創(chuàng)建配置類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); }}

輸出結(jié)果

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

-總結(jié) 二者的一些區(qū)別

特性 @ConfigurationProperties @Value SpEL表達(dá)式 不支持 支持 屬性松散綁定 支持 不支持 JSR303數(shù)據(jù)校驗 支持 不支持

添加校驗注解

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

輸出結(jié)果

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這兩個注解默認(rèn)從項目的主配置文件中讀取配置,當(dāng)項目配置較多全部從一個地方讀取會顯得臃腫,可以將配置文件按照模塊拆分讀取到不同的配置類中,可以使用@PropertySource配合@Value讀取其他配置文件

代碼樣例

創(chuàng)建配置文件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

創(chuàng)建配置類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); }}

輸出結(jié)果

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

小結(jié)

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

總結(jié)

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
麻豆精品视频在线| 国产毛片精品| 成人黄色av| 精品视频免费| 久久精品成人| 日韩精品三级| 日本亚洲最大的色成网站www| 国产精品日本| 亚洲综合不卡| 99视频在线精品国自产拍免费观看| 亚洲专区视频| 国产精品22p| 日韩中文首页| 国产日韩亚洲| 欧美+日本+国产+在线a∨观看| 久久青青视频| 日韩欧美字幕| 中文另类视频| 国产精品尤物| 高清日韩欧美| 爽爽淫人综合网网站| 99在线观看免费视频精品观看| 国产一区二区三区视频在线| 人人精品久久| 精品免费av| 亚洲深夜av| 国产91在线播放精品| 免费在线成人| 国产欧美一区二区三区米奇| 国精品产品一区| 国产黄色精品| 亚洲色图国产| 97精品一区| 日韩视频二区| 99国产成+人+综合+亚洲欧美| av资源中文在线天堂| 91精品国产成人观看| 精品一区二区三区中文字幕视频 | 狠狠久久婷婷| 日本天堂一区| 蜜桃tv一区二区三区| 欧美日韩在线精品一区二区三区激情综合 | 精品1区2区3区4区| 国产精品密蕾丝视频下载| 国产一区欧美| 高清一区二区| 久久国产精品免费一区二区三区| 都市激情国产精品| 日韩av电影一区| 久久国产88| 亚洲福利久久| 国产高清不卡| 精品久久99| 麻豆91精品视频| 日韩精品一区二区三区中文| 国产91一区| 日韩精品永久网址| 日韩.com| 国产传媒av在线| 免费看一区二区三区| 国产欧美激情| 婷婷综合成人| 欧美专区18| 女人天堂亚洲aⅴ在线观看| 日韩欧美一区二区三区免费看| 国产伦一区二区三区| 欧美综合国产| 蜜桃精品在线| 在线精品小视频| 午夜久久福利| 在线免费观看亚洲| 日韩精品一区二区三区av| 天堂俺去俺来也www久久婷婷| 最新国产精品久久久| 亚洲精选久久| 久久精品一区| 亚洲免费成人av在线| 亚洲久久在线| 91福利精品在线观看| 久久精品一区二区三区中文字幕| 国产中文欧美日韩在线| 91精品一区二区三区综合在线爱| 亚洲激精日韩激精欧美精品| 亚洲一区二区三区久久久| 欧美日韩视频免费看| 国产一区三区在线播放| 欧美国产91| 日韩二区三区四区| 都市激情国产精品| 免费视频一区二区| 精品三级久久久| 亚洲一区国产一区| 欧美精品国产| 日本精品不卡| 欧美日本久久| 久久麻豆精品| 国产精品扒开腿做爽爽爽软件| 99久久99久久精品国产片果冰| 亚洲精品动态| 99久久精品国产亚洲精品| 久久精品凹凸全集| 在线亚洲成人| 日韩国产一区二区三区| 国产日韩欧美在线播放不卡| 亚洲国产专区| 精品国产欧美| 88久久精品| 亚洲作爱视频| 日本国产精品| 精品国产三区在线| 欧美有码在线| 最新国产精品| 一区在线免费| 91精品高清| 婷婷综合六月| 国产一区二区精品福利地址| 日韩免费精品| 午夜亚洲福利| 国产模特精品视频久久久久| 久久久人人人| 久久蜜桃资源一区二区老牛| 久久影院资源站| 国产欧美一区二区色老头| 91精品1区| 丝袜诱惑制服诱惑色一区在线观看| 日韩中文在线播放| 亚洲视频综合| 亚洲自啪免费| 免费高清在线一区| 国产日韩综合| 麻豆9191精品国产| 综合激情在线| 视频一区二区欧美| 日韩精品一区二区三区av| 亚洲精品进入| 久久不卡日韩美女| 成人午夜在线| 日韩成人三级| 精品不卡一区| 136国产福利精品导航网址| 中文日韩在线| 日韩成人一级| 成人亚洲一区二区| 国产一区日韩欧美| 亚洲3区在线| 国内精品亚洲| 99国产精品99久久久久久粉嫩| 亚洲精品美女| 美女久久久久久| 99视频精品全国免费| 亚洲一区二区三区高清不卡| 国产午夜一区| 成人在线网站| 国产午夜精品一区二区三区欧美| 一级欧美视频| 成人av三级| 中文字幕免费一区二区| 国产激情精品一区二区三区| 久久九九精品| 亚洲制服欧美另类| 精品国产免费人成网站| 亚洲精品在线a| 久久精品免费一区二区三区| 视频一区二区中文字幕| 色欧美自拍视频| 日韩精品视频一区二区三区| 中文字幕人成乱码在线观看| 蜜桃av一区二区三区电影| 国产精品美女在线观看直播| 国产精品av久久久久久麻豆网| 欧美天堂一区| 国产亚洲毛片| 日本欧美不卡| 成人日韩av| 国产欧美在线观看免费| 一区免费在线| 亚洲黄色网址| 国产精品黄网站| 亚洲色图网站| 欧美精品一二| 日韩在线综合| 国产传媒在线观看| 精品久久影院| 日韩精品高清不卡| 日韩一区欧美二区| 久久成人精品| 在线亚洲成人| 国产精品毛片在线| 久久精品播放| 三上悠亚国产精品一区二区三区| 国产九九精品| 欧美日韩一区二区三区不卡视频 | 樱桃成人精品视频在线播放| 成人免费网站www网站高清| 成人午夜网址| 久久裸体视频| 亚洲精品网址| 蜜臀av国产精品久久久久| 香蕉久久一区| 国产精品最新自拍|