springMvc 之 【javaBean读取属性配置文件中的值】---注解方式

来源:互联网 发布:flag在c语言中的意思 编辑:程序博客网 时间:2024/05/16 02:08

如果在javaBean中读取属性配置文件中的值呢?方法如下:


1、属性配置文件【application.properties】中的部分键值对如下:

# qidao FTP configqidaoFTPHost=10.10.6.12qidaoFTPPort=21qidaoFTPDir=/qidaoFTPUsername=qi2daoqidaoFTPPwd=qi2daoshejiFTPUsername=she2jishejiFTPPwd=she2jiftpDownloadStorePath=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;@Servicepublic 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
原创粉丝点击