1. 程式人生 > >Spring載入Properties配置檔案,java通過註解讀取資料

Spring載入Properties配置檔案,java通過註解讀取資料

 1、用法示例: 在springMVC.xml配置檔案中新增標籤

(推薦用這個,這個用的比較多,也比較簡潔)

<context:property-placeholder location="classpath:salesman.properties"/>

 載入多個

<context:property-placeholder location="classpath:*.properties"/>

在Java中使用這個@Value("${ }")註解 讀取 properties中的引數

	@Value("${filePath}") 
	private String filePath;
	public void setFilePath(String filePath) {
		System.out.println(filePath);
		this.filePath = filePath;
}

 在其他配置檔案中使用 ${ } 讀取 properties中的引數

<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="SMSSDetail_1" />
        </property>
        <!-- 每一秒鐘執行一次 -->
        <property name="cronExpression">
            <value>${SMSSCHEDUlERTIME}</value>
        </property>
    </bean>

 2.方式是使用註解的方式注入,主要用在java程式碼中使用註解注入properties檔案中相應的value值 ,

   注意:變數不能用static修飾;

<!-- 第二種方式是使用註解的方式注入,主要用在java程式碼中使用註解注入properties檔案中相應的value值 -->
 <bean id="PropertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 	<property name="locations"><!-- 這裡是PropertiesFactoryBean類,它也有個locations屬性,也是接收一個數組,跟上面一樣
 		<array>
 			<value>classpath:salesman.properties</value>
 		</array>
 	</property>
    <property name="fileEncoding" value="UTF-8"/>
 </bean>



@Component("fileUpload")
public class FileUploadUtil implements FileUpload {

	@Value("#{PropertiesFactory.filePath}") 
	private String filePath;
	//@Value表示去beans.xml檔案中找id="PropertiesFactory"的bean,它是通過註解的方式讀取properties配置檔案的,然後去相應的配置檔案中讀取key=filePath的對應的value值
	public void setFilePath(String filePath) {
		System.out.println(filePath);
		this.filePath = filePath;

 3.掃描多個配置檔案,把需要掃描的檔案放在新建的conf檔案下:

在D:\Project\tc_vsmp\src-main-resources\applicationContext.xml 檔案中配置bean 掃描

<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	<property name="locations">
 		<array>
 			<value>classpath:conf/*</value>
 		</array>
 	</property>
	<property name="fileEncoding" value="UTF-8"/>
 </bean>

java類讀取:

 public class SMSScheduler {
    /**
     * 
    @Autowired
    private LoanQueryService service;

    /**
     * 
     */
    @Value("#{prop.TIMEDPUSHDAY}")
    public String TIMEDPUSHDAY;
     /**
     *
     */
    @Value("#{prop.SMSSCHEDUlER_JINXING_IPHONENUM}")
    public String SMSSCHEDUlER_JINXING_IPHONENUM;

在配置檔案中讀取:

<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="SMSSDetail_1" />
        </property>
        <!-- 每一秒鐘執行一次 -->
        <property name="cronExpression">
            <value>#{prop.SMSSCHEDUlERTIME}</value>
        </property>
    </bean>

相關推薦

Spring載入Properties配置檔案java通過註解讀取資料

 1、用法示例: 在springMVC.xml配置檔案中新增標籤 (推薦用這個,這個用的比較多,也比較簡潔) <context:property-placeholder location="classpath:salesman.properties"/>

Spring載入Properties配置檔案的加密解密處理

需求場景:加密Properties配置檔案中的資料庫連線字串和使用者名稱、密碼 實現思路:重寫PropertyPlaceholderConfigurer類中的processProperties方法,在讀取配置資訊之前實現解密 一、PropertyPlaceholderCon

Springproperties配置檔案注入

1. 使用PropertiesFactoryBean 配置檔案 <bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name

載入properties配置檔案

需要jar包 工具類: package qingxia.tang.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.springframew

在工廠模式中使用Properties配置檔案出現java.lang.ExceptionInInitializerError的問題

工廠模式的概念不提了 在使用Properties配置檔案時,使用FileInputStream載入配置檔案 public class DaoFactory { private ArchiveDao archiveDao; privat

java模擬表單上傳檔案java通過模擬post方式提交表單實現圖片上傳功能例項

package com.zdz.httpclient;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.

spring mvc 怎麼指定spring.xml的配置檔案在web.xml中指定

web.xml檔案配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://

Spring 載入xml配置檔案的方式 ApplicationContext

       大家都知道Java讀普通檔案是通過Basic I/O 中的InputStream、OutStream、Reader、Writer 等實現的。在spring 框架中,它是怎樣識別xml這個配置檔案的呢? 這就要靠IoC容器的兩個介面BeanFactory 和Ap

springboot讀取非classpath下配置檔案並且支援註解使用

springboot載入配置檔案路徑:src下為classpath,src同級目錄為file。 @SpringBootApplication @PropertySource(value = {"file:config/application.yml","file:con

通過Spring讀取properties配置檔案

一般properties檔案,用於在程式碼塊中讀取,並給變數賦值。但是,Spring可以Bean XML方式定義(註冊Bean)中,可以通過${屬性名}使用properties檔案配置的值。或者在程式碼中使用@Value註解讀取properties的屬性值。 所以用途有兩種

springmvc通過@Value註解讀取Properties配置檔案的值junit測試可以取到值但是在業務中無法讀取

最近試著做了個springmvc 專案,在加入 發郵件 功能時遇到的問題。 Spring 通過註解獲取*.porperties檔案的內容,除了xml配置外,還可以通過@value方式來獲取。 @value是需要spring註解掃描的,所以要將spring註解掃描配置中加上實

實現spring mvc 的java config 載入不同配置檔案

需要配置一個spring 啟動時的事件監聽器. SpringRootAppInitListener.java 配置類 /** * spring 根容器啟動時的監聽事件 * * @return Application

Jenkins 通過maven打包時發現resource下的properties 配置檔案未生成 導致在執行時報錯

場景: Jenkins 構建WebDriver 專案時,報如下圖錯誤: idea 執行時是沒問題的,經過對比發現是因為Jenkins構建時,發現resource下的properties 配置檔案未生成,找到不所以才提示錯誤 解決方法: 程式碼路徑: 在pom.x

log4j的使用與javaproperties配置檔案載入

    日誌是我們在寫程式碼中經常會用到的,程式出錯了我們也需要去檢視日誌來調錯,對於像我們這一些新人來說,怎麼去使用日誌就比較陌生,下面我將我學習的過程分享一下: 1.需要找到一個log4j包,我使用的是log4j-1.2.15.jar。放在工程lib資料夾下 2.新建一

spring載入外部properties配置檔案

在配置spring的時候有些配置屬性放到外部檔案中進行管理或許更合理,這種情況一般是在配置資料來源的時候,將資料來源的配置資訊儲存在配置檔案中,然後在spring中載入配置檔案讀取具體的值進行bean屬性的注入,這樣更有利於管理我們的配置資訊。spring是通過一個以實現的

Spring註解驅動開發】使用@PropertySource載入配置檔案我只看這一篇!!

## 寫在前面 > 很多小夥伴都在問:冰河,你的Spring專題更新完了嗎?怎麼感覺像是寫了一半啊?我:沒有更新完呀,整個專題預計會有70多篇。那怎麼更新了一半就去寫別的了呢?那是因為有很多其他的小夥伴在後臺留言說:急需學習一些其他的技術,所以,臨時調整的。放心,Spring專題會持續更新的!這不,今

Spring boot 配置https 實現java通過https介面訪問

      近來公司需要搭建一個https的伺服器來除錯介面(伺服器用的spring boot框架),剛開始接觸就是一頓百度,最後發現網際網路認可的https安全連結的證書需要去CA認證機構申請,由於是除錯階段就採用了java的keytool工具來生成金鑰檔案

根據properties配置檔案獲取裡面的鍵值對jfinal原始碼分析

在jfinal框架中,有一個Prop的類,該類提供對properties配置檔案裡獲取鍵值對的功能。 其底層還是用的是java.util.Properties的相關方法,只不過是人家進行了封裝而已。牛人都喜歡封裝原有的東西。 其建構函式Prop(String fileName,Str

java初始化配置檔案 直接使用PropUtil.get(key)獲取值

package com.audaque.cas.server; import org.slf4j.Logger;   import org.slf4j.LoggerFactory;      import java.io.*;  

java selenium 讀取配置檔案報錯中文亂碼

參考引自:https://blog.csdn.net/qq_27093465/article/details/70765870 根據自己問題解決: package com.property; import java.io.BufferedInputStream; import java.i