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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值