Question:
The following two configuration items are configured in application.properties
ccb.ip.address=10.25.177.31
ccb.ip.port=1600
The configuration in the application.properties configuration file cannot be read in the following problem code
Question code:
public class BISFrontFileUtil {
private static Logger logger = LoggerFactory.getLogger(BISFrontFileUtil.class);
@Value("${ccb.ip.address}")
private static String CCBIPADDRESS;
@Value("${ccb.ip.port}")
private static int CCBIPPORT;
public static boolean putFileToFront(String fileName, String fileContentStr) {
boolean flag = false;
try {
String clientFlag = "in"; // in for ping an internal system
logger.info("For the new generation of CCB, the file name uploaded to the front-end computer:" + fileName + ",bis Of IP Address:" + CCBIPADDRESS + ",Port number:" + CCBIPPORT);
InputStream in = new ByteArrayInputStream(fileContentStr.getBytes("UTF-8"));
ResultBean result = BankFileHandleServiceImp.sendBankStream(in,
fileContentStr.getBytes().length, fileName, CCBIPADDRESS, CCBIPPORT, clientFlag);
String resultCode = result.getPA_RSLT_CODE();// Get uploaded results
String resultMsg = result.getPA_RSLT_MESG();
if (resultCode.equals(ResultCodeUtil.CODE_999999)) {// 999999 indicates successful upload
logger.info("CCB new generation, upload front-end file" + fileName + "Success");
flag = true;
} else {
logger.info("CCB new generation failed to upload the front-end file, failure code:" + resultCode + ",Failure information:" + resultMsg);
}
} catch (Exception e) {
logger.error("CCB new generation upload front-end content conversion UTF-8 Form error:"+ e, e);
}
return flag;
}
}
Result:
After running, it was found that CCBIPADDRESS was null and CCBIPPORT was 0 (int was 0 by default).
Reason:
Later, google found that spring boot did not allow / support injecting values into static variables.
Correction method:
spring boot supports set method injection. We can inject static variables with non static set method
Correction code:
@Component
public class BISFrontFileUtil {
private static Logger logger = LoggerFactory.getLogger(BISFrontFileUtil.class);
private static String CCBIPADDRESS;
private static int CCBIPPORT;
@Value("${ccb.ip.address}")
public void setCCBIPADDRESS(String cCBIPADDRESS) {
CCBIPADDRESS = cCBIPADDRESS;
}
@Value("${ccb.ip.port}")
public void setCCBIPPORT(int cCBIPPORT) {
CCBIPPORT = cCBIPPORT;
}
/**
* Upload files to bis front end
* @param fileName
* @param fileContentStr
* @return
*/
public static boolean putFileToFront(String fileName, String fileContentStr) {
boolean flag = false;
try {
String clientFlag = "in"; // in for ping an internal system
logger.info("For the new generation of CCB, the file name uploaded to the front-end computer:" + fileName + ",bis Of IP Address:" + CCBIPADDRESS + ",Port number:" + CCBIPPORT);
InputStream in = new ByteArrayInputStream(fileContentStr.getBytes("UTF-8"));
ResultBean result = BankFileHandleServiceImp.sendBankStream(in,
fileContentStr.getBytes().length, fileName, CCBIPADDRESS, CCBIPPORT, clientFlag);
String resultCode = result.getPA_RSLT_CODE();// Get uploaded results
String resultMsg = result.getPA_RSLT_MESG();
if (resultCode.equals(ResultCodeUtil.CODE_999999)) {// 999999 indicates successful upload
logger.info("CCB new generation, upload front-end file" + fileName + "Success");
flag = true;
} else {
logger.info("CCB new generation failed to upload the front-end file, failure code:" + resultCode + ",Failure information:" + resultMsg);
}
} catch (Exception e) {
logger.error("CCB new generation upload front-end content conversion UTF-8 Form error:"+ e, e);
}
return flag;
}
}
Be careful:
1. Fix @ Component in code not to be lost
2. If the set method is not static