1. 程式人生 > >spring註解的方式讀取properties檔案中的內容

spring註解的方式讀取properties檔案中的內容

在spring的配置檔案ApplicationContext.xml中加入.

<!-- 載入所有的properties檔案,方便類中用spring註解方式獲取 -->

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/properties/*.properties</value>
</list>
</property>

</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/jdbc.properties" />
<property name="properties" ref="configProperties" />
</bean>

需要用的類裡面如下宣告:

private String name;

public String getName() {
return name;
}

@Value("#{configProperties[name]}")
public void setName(String name) {
this.name = name;
}

意思就是讀取*.properties檔案中key為name的值,如果有多個properties檔案。預設會合並,所以不需要關注是哪個properties中的key,如果多個properties中有重名的key,預設後面的會覆蓋前面的。

相關推薦

spring註解方式讀取properties檔案內容

在spring的配置檔案ApplicationContext.xml中加入. <!-- 載入所有的properties檔案,方便類中用spring註解方式獲取 --> <bean id="configProperties" class="org.spri

spring xml讀取Properties檔案的加密欄位

spring的xml配置檔案,能夠方便地讀取properties檔案中的值。 讀取單個屬性檔案: <bean class="org.springframework.beans.factory.c

使用@ConfigurationProperties和類的方式讀取yml檔案的值

問題:yml檔案的屬性如下 nuoche: privateNumber: appkey: 7L57717mzPP6cr appSecret: iWoVQ5s4kr75D domainName: https://www.caocao.com 如何通過@Confi

Spring註解方式和配置檔案方式的混用

1.註解方式和配置檔案方式的混用:     1.配置檔案方式 : 用於配置物件的建立          <beans>             <!--開啟掃描-->             <context:component-scan bas

java讀取 *.properties檔案所有物件

這裡提供解析的輔助方法 package com.cms.util; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.Inpu

在ServletContextListener 的實現類(使用Spring @Value 註解方式讀取配置檔案、或者注入Spring bean)

在ServletContextListener 的實現類中 使用Spring @Value 註解的方式讀取配置檔案 我想向ServletContextListener中通過Spring @value 的方法讀取 properties 配置檔案資訊,但是我開始的方法不行 public class MyLi

五種方式讓你在java讀取properties檔案內容不再是難題

一、背景   最近,在專案開發的過程中,遇到需要在properties檔案中定義一些自定義的變數,以供java程式動態的讀取,修改變數,不再需要修改程式碼的問題。就藉此機會把Spring+SpringMVC+Mybatis整合開發的專案中通過java程式讀取properties檔案內容的方式進行了梳理和分析

五種方式讓你在java讀取properties文件內容不再是難題

toolbar ota oca vat include tor 代碼塊 location interface 一、背景   最近,在項目開發的過程中,遇到需要在properties文件中定義一些自定義的變量,以供java程序動態的讀取,修改變量,不再需要修改代碼的問題。就借

javaEE Springmvc,properties檔案解決硬編碼問題,@Value註解獲取properties檔案內容

springmvc.xml(Springmvc的核心配置檔案,讀取properties檔案): <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframewor

springboot 讀取配置檔案的變數(通過註解方式

springboot的application.properties檔案中可以定義一些可配置的常量。在程式中我們不需要再重新的讀取檔案,我們可以直接使用@Value註解讀取配置檔案中的值。首先看一下配置檔案application.properties中的內容是:spring.p

spring boot 程式碼、註解配置獲取yml、properties檔案的map即鍵值對

一、yml獲取自定義鍵值對 yml中的鍵值對 test: map: key1: value1 key2: value2 key3: value3 pom中的依賴配置 <dependency> <groupId>org.sprin

Spring配置 在xml和java程式碼讀取properties檔案

在spring引入屬性檔案 <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

java讀取檔案內容,如讀取.properties檔案

有兩種方法來讀取檔案中的內容 方法一: 1.jdbc.properties檔案 oracle.driverClassName=oracle.jdbc.driver.OracleDriver ora

spring提供讀取配置檔案的屬性註解@Value

有時,當我們需要把專案中的properties檔案的屬性可以在專案中得到,spring就提供了一個註解@Value來讀取(前提是屬性檔案properties需要首先讓spring管理,即spring配置檔案中包含一下) @Value("${屬性名}")          /

java讀取properties檔案內容

package com.tgb.SpringActivemq.utils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; impo

Spring配置檔案讀取properties檔案的屬性

一般我們會將關於資料庫的配置屬性存放在一個獨立的properties檔案 以下是屬性檔案anyview.properties anyview.driverClassName=com.mysql.jdbc.Driver anyview.url=jdbc:mysql://lo

Spring 如何讀取properties檔案內容

http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html 在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據我工作中用到的讀取properties配置檔案的方法小

SpringBoot讀取properties檔案方式和@Bean註解

application.properties檔案#######################方式一######################### com.zyd.type3=Springboot - @ConfigurationProperties com.zyd.ti

spring boot框架學習之重要註解3註解方式讀取外部資源配置文件

凱哥java java註解 本節主要內容:1:是用非註解方式怎麽獲取配置文件中的配置項2:使用註解實戰獲取外部properties文件配置項聲明:本文是《凱哥陪你學系列-框架學習之spring boot框架學習》中spring boot框架學習學前掌握之重要註解(3)-通過註解方式讀取外部資源配置文件

spring boot框架學習學前掌握之重要註解(4)-通過註解方式讀取外部資源配置文件2

spring boot kaigejava 凱哥java本節主要內容:1:思考問題:怎麽讀取多個配置文件,如果文件不存在怎麽辦2:配置數據庫連接池聲明:本文是《凱哥陪你學系列-框架學習之spring boot框架學習》中spring boot框架學習學前掌握之重要註解(4)-通過註解方式讀取外部資源配置文件2