1. 程式人生 > >springMvc 之 【javaBean讀取屬性配置檔案中的值】---註解方式

springMvc 之 【javaBean讀取屬性配置檔案中的值】---註解方式

如果在javaBean中讀取屬性配置檔案中的值呢?方法如下:

1、屬性配置檔案【application.properties】中的部分鍵值對如下:

# qidao FTP config
qidaoFTPHost=10.10.6.12
qidaoFTPPort=21
qidaoFTPDir=/
qidaoFTPUsername=qi2dao
qidaoFTPPwd=qi2dao
shejiFTPUsername=she2ji
shejiFTPPwd=she2ji
ftpDownloadStorePath=D:/新建資料夾
2、在JavaBean裡面獲取屬性配置檔案中的值,需注意滿足以下條件:
  • 必須是在自動依賴注入的類中, 比如@Service註解標識的類;
  • 獲取的方式:@Value("${鍵}"),比如@Value("${qidaoFTPUsername}"),將值賦值給@Value註解下面對應的屬性。
package com.openeap.modules.taskBoard.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FtpConfigService{
	
	//任務看板啟道FTP的使用者名稱和密碼
	@Value("${qidaoFTPUsername}")
	private String qdUserName;
	
	@Value("${qidaoFTPPwd}")
	private String qdUserPassword;
	
	//任務看板設計FTP的使用者名稱和密碼
	@Value("${shejiFTPUsername}")
	private String sjUserName;
	
	@Value("${shejiFTPPwd}")
	private String sjUserPassword;
	
	//任務看板FTP的IP地址
	@Value("${qidaoFTPHost}")
	private String ftpIp;
	
	//任務看板FTP埠號
	@Value("${qidaoFTPPort}")
	private int ftpPort;
	
	//任務看板FTP下載儲存路徑
	@Value("${ftpDownloadStorePath}")
	private String ftpDownloadStorePath;
	
	//任務看板FTP遠端路徑
	@Value("${qidaoFTPDir}")
	private String qidaoFTPDir;
	
	//FTP索引檔案
	private String ftpIndexFileName = "FtpFileInfo.xml";

	
	//=====================================set and get=====================================================
	
	public String getQdUserName() {
		return qdUserName;
	}

	public void setQdUserName(String qdUserName) {
		this.qdUserName = qdUserName;
	}

	public String getQdUserPassword() {
		return qdUserPassword;
	}

	public void setQdUserPassword(String qdUserPassword) {
		this.qdUserPassword = qdUserPassword;
	}

	public String getSjUserName() {
		return sjUserName;
	}

	public void setSjUserName(String sjUserName) {
		this.sjUserName = sjUserName;
	}

	public String getSjUserPassword() {
		return sjUserPassword;
	}

	public void setSjUserPassword(String sjUserPassword) {
		this.sjUserPassword = sjUserPassword;
	}

	public String getFtpIp() {
		return ftpIp;
	}

	public void setFtpIp(String ftpIp) {
		this.ftpIp = ftpIp;
	}

	public int getFtpPort() {
		return ftpPort;
	}

	public void setFtpPort(int ftpPort) {
		this.ftpPort = ftpPort;
	}

	public String getFtpDownloadStorePath() {
		return ftpDownloadStorePath;
	}

	public void setFtpDownloadStorePath(String ftpDownloadStorePath) {
		this.ftpDownloadStorePath = ftpDownloadStorePath;
	}

	public String getFtpIndexFileName() {
		return ftpIndexFileName;
	}

	public void setFtpIndexFileName(String ftpIndexFileName) {
		this.ftpIndexFileName = ftpIndexFileName;
	}

	public String getQidaoFTPDir() {
		return qidaoFTPDir;
	}

	public void setQidaoFTPDir(String qidaoFTPDir) {
		this.qidaoFTPDir = qidaoFTPDir;
	}
	
	
	
	
	
}