1. Set by annotating @value for each. This case defaults to reading the contents of the application-*.properties file, for example setting the boot from dev mode
package com.esoon.ele.web.config;/**
* Created by burns.
*
* @author <a href="http://www.esoon-soft.com/">burns</a>
* @date2021/06/24 11:32 * /
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* TODO
*
* @ClassName appModel
* @Author Burns
* @DAte2021/6/24 11:32 * /
@Component
@Data
public class AppModel {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
@Value("${app.info}")
private String appInfo;
@Value("${app.number}")
private String appNumber;
}
Copy the code
2. The second way is that the Java class attributes and application-*.properties are identical except for the prefix, and the configuration in the configuration file is defined in the Java class annotations
package com.esoon.ele.web.config;/**
* Created by burns.
*
* @author <a href="http://www.esoon-soft.com/">burns</a>
* @date 2021/06/24 13:51
*/
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* TODO
*
* @ClassName HomeProperties
* @Author Burns
* @DAte 2021/6/24 13:51
*/
@Data
@Component
@ConfigurationProperties(prefix = "home")
public class HomeProperties {
/** ** province */
private String province;
/** * city */
private String city;
/** * description */
private String desc;
}
Copy the code
3. The third way is to read from another common configuration file, either in the classpath or in another common path for example
/ * * *@Title: ConfigProperties.java
* @Package com.acconsys.ids.util
* @Description: TODO(a one-sentence description of what the file does) *@author 35725
* @dateJanuary 8, 2020 9:20:40 am *@versionV1.0 * /
package com.esoon.ids.common;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/ * * *@ClassName: ConfigProperties
* @Description: TODO(here is a one-sentence description of what this class does) *@author 35725
* @date8 January 2020 * */
@Component
//@PropertySource(value = "classpath:config/config.properties")
@PropertySource(value="file:${cis.properties}")
public class CisProperties {
@Value("${cis.ip}")
private String ip;
@Value("${cis.port}")
private int port;
@Value("${cis.username}")
private String username;
@Value("${cis.password}")
private String password;
/ * * *@valueAnnotations inject properties 1. Use above the class name@PropertySource(" CLASspath :*") * annotation,* stands for properties file path, can point to multiple configuration file path * if multiple configuration files, yes@PropertySource({"classpath:*","classpath:*".... }) 2. * is used directly on fields@valueNote 3. The annotations in using ${core. The pool. The size} core. The pool. The size on behalf of the properties file inside the key 5. Need to add PropertySourcesPlaceholderConfigurer bean 6 *. In the * PropertySourcesPlaceholderConfigurer is increased@beanAnnotation stating that a bean is returned, otherwise injection will fail * */
// @Bean
// public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
// return new PropertySourcesPlaceholderConfigurer();
// }
public String getIp(a) {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort(a) {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername(a) {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword(a) {
return password;
}
public void setPassword(String password) {
this.password = password; }}Copy the code