First kind
1 yml content
2. Create a new Java configuration class
@Configuration @ConfigurationProperties(prefix = "ftp") @Data public class FtpConfig { /** * Connection of static resource presentation */ private String showUrl; /** * sftp Server storage path */ private String serverFilePath; /** * sftp Server IP */ private String ip; /** * sftp user name */ private String userName; /** * sftp User password */ private String password; /** * sftp port */ private int port; /** * Upload file size limit, unit B */ private int maxFileSize; }
3 used in the program
private UploadFileResp upload(MultipartFile file) { try { // file name String originFileName = file.getOriginalFilename(); String[] fNames = originFileName.split("\\."); String suffix = fNames.length >= 1 ? fNames[fNames.length - 1] : ""; String fileName = TimeUtil.getDateString(new Date(), TimeUtil.FORMAT_YMDHMS) + "." + suffix; // File bytecode byte[] bytes = null; bytes = file.getBytes(); Long fileSize = file.getSize(); if (fileSize > ftpConfig.getMaxFileSize()) { return new UploadFileResp(false, "The uploaded file is too large and has exceeded" + ftpConfig.getMaxFileSize() / 1024 / 1024 + "M limit"); } SftpUtil ftpUtil = new SftpUtil(ftpConfig.getIp(),ftpConfig.getPort(), ftpConfig.getUserName(), ftpConfig.getPassword(), ftpConfig.getServerFilePath()+ File.separator + TimeUtil.getDateString(new Date(), TimeUtil.FORMAT_YMD)); ftpUtil.upLoadFile(fileName, bytes); return new UploadFileResp(true, ftpConfig.getShowUrl() + File.separator + fileName); } catch (Exception e) { e.printStackTrace(); return new UploadFileResp(false, "fail"); } }
The second part} explains in detail several ways of spring boot reading yml configuration
yml file rules
- The benefits of the yml file, the natural tree structure, are clear at a glance, and are essentially similar to properties.
- tab indentation is not supported
- You can use "- lowercase letters" or "lowercase letters" instead of "uppercase letters", such as userName and user name, user_ Name has the same meaning
key: value format writing
The key is followed by a colon, followed by a space, and then the value
Representation of several data formats
1. Ordinary value (number, string, Boolean)
2. Object, Map (attribute and value) (key value pair)
3. Array (List, Set)
Normal value (number, string, Boolean)
Directly refers to key: value, such as:
1 2 | age: 18 name: root |
Note:
The string does not need single quotation marks or double quotation marks by default;
"": double quotation marks; Special characters in the string will not be escaped; Special characters will be used as the meaning they want to express
name: "zhangsan \n lisi": output; Line feed lisi
'': single quotation mark; Special characters will be escaped, and the special characters will eventually be just ordinary string data
name: 'Zhangsan \ n Lisi': output; zhangsan \n lisi
Object, Map (attribute and value) (key value pair)
Object or k: v
k: v: write the relationship between the attribute and value of the object in the next line; Pay attention to indentation (tab is not supported, and spaces are used), such as:
1 2 3 | person: age: 18 name: root |
Array (List, Set)
Use the - value to represent an element in the array, such as:
1 2 3 | hands: - left - right |
First reading method @ value
If we only need one or two values in the configuration file, @ Value , is the simplest and most convenient way
1 2 | server: port: 9001 |
We can take this value in the code
1 2 | @Value("${server.port}") public String port; |
Note: the class of prot here needs to be a Component. If it is an entity class, it needs to add @ Component
The second reading method is @ ConfigurationProperties
If we need a JavaBean to map the configuration, we usually use @ ConfigurationProperties to read it
1 2 3 | student: age: 18 name: root |
javabean:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @Component @ConfigurationProperties(prefix = "student") public class Student {
private String name;
private Integer age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } |
To use @ ConfigurationProperties, you need to configure a prefix parameter, that is, write key
The third reading method @ Environment
This method seems to be used less, and has not been used
1 2 | test: msg: root is a good student |
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support the script home.