我想访问应用程序中提供的值。属性,例如:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
我想访问userBucket。在Spring Boot应用程序的主程序中的路径。
我想访问应用程序中提供的值。属性,例如:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
我想访问userBucket。在Spring Boot应用程序的主程序中的路径。
当前回答
为了从属性文件中选择值,我们可以有一个配置读取器类,比如ApplicationConfigReader.java 然后根据属性定义所有变量。参考下面的例子,
application.properties
myapp.nationality: INDIAN
myapp.gender: Male
下面是相应的阅读类。
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapp")
class AppConfigReader{
private String nationality;
private String gender
//getter & setter
}
现在,我们可以在任何想要访问属性值的地方自动连接阅读器类。 如。
@Service
class ServiceImpl{
@Autowired
private AppConfigReader appConfigReader;
//...
//fetching values from config reader
String nationality = appConfigReader.getNationality() ;
String gender = appConfigReader.getGender();
}
其他回答
Spring-boot允许我们使用几种方法来提供外部化配置,您可以尝试使用application。Yml或yaml文件代替属性文件,并根据不同的环境提供不同的属性文件设置。
我们可以将每个环境的属性分离到单独的spring概要文件下的单独的yml文件中。然后在部署期间,您可以使用:
java -jar -Drun.profiles=SpringProfileName
指定要使用哪个弹簧概要文件。注意,yml文件的名称应该类似
application-{environmentName}.yml
让他们自动被新兵带走。
参考资料:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties
从申请中读取。Yml或属性文件:
从属性文件或yml中读取值的最简单方法是使用spring @value注释。Spring会自动将yml中的所有值加载到Spring环境中,所以我们可以直接从环境中使用这些值,比如:
@Component
public class MySampleBean {
@Value("${name}")
private String sampleName;
// ...
}
或者spring提供的另一个读取强类型bean的方法如下:
YML
ymca:
remote-address: 192.168.1.1
security:
username: admin
对应的POJO读取yml:
@ConfigurationProperties("ymca")
public class YmcaProperties {
private InetAddress remoteAddress;
private final Security security = new Security();
public boolean isEnabled() { ... }
public void setEnabled(boolean enabled) { ... }
public InetAddress getRemoteAddress() { ... }
public void setRemoteAddress(InetAddress remoteAddress) { ... }
public Security getSecurity() { ... }
public static class Security {
private String username;
private String password;
public String getUsername() { ... }
public void setUsername(String username) { ... }
public String getPassword() { ... }
public void setPassword(String password) { ... }
}
}
上述方法适用于yml文件。
参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
有两种方法,
你可以直接在类中使用@Value
@Value("#{'${application yml field name}'}")
public String ymlField;
OR
要使它干净,你可以清除@Configuration类,在那里你可以添加所有的@value
@Configuration
public class AppConfig {
@Value("#{'${application yml field name}'}")
public String ymlField;
}
我也有这个问题。但是有一个很简单的解决办法。只需要在构造函数中声明你的变量。
我的例子:
application.propperties:
#Session
session.timeout=15
SessionServiceImpl经济舱:
private final int SESSION_TIMEOUT;
private final SessionRepository sessionRepository;
@Autowired
public SessionServiceImpl(@Value("${session.timeout}") int sessionTimeout,
SessionRepository sessionRepository) {
this.SESSION_TIMEOUT = sessionTimeout;
this.sessionRepository = sessionRepository;
}
获取财产价值的最佳方法是使用。
1. 使用Value注释
@Value("${property.key}")
private String propertyKeyVariable;
2. 使用环境bean
@Autowired
private Environment env;
public String getValue() {
return env.getProperty("property.key");
}
public void display(){
System.out.println("# Value : "+getValue);
}
应用程序可以从应用程序中读取3种类型的值。属性文件。
application.properties
my.name=kelly
my.dbConnection ={connection_srting:'http://localhost:...',username:'benz',password:'pwd'}
类文件
@Value("${my.name}")
private String name;
@Value("#{${my.dbConnection}}")
private Map<String,String> dbValues;
如果你在申请中没有房产。属性,则可以使用默认值
@Value("${your_name : default value}")
private String msg;