我想访问应用程序中提供的值。属性,例如:
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应用程序的主程序中的路径。
当前回答
你也可以这样做....
@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {
@Autowired
private Environment env;
public String getConfigValue(String configKey){
return env.getProperty(configKey);
}
}
然后无论你想从应用程序中读取什么。属性,只需将键传递给getConfigValue方法。
@Autowired
ConfigProperties configProp;
// Read server.port from app.prop
String portNumber = configProp.getConfigValue("server.port");
其他回答
应用程序。Yml或application.properties
config.value1: 10
config.value2: 20
config.str: This is a simle str
MyConfig类
@Configuration
@ConfigurationProperties(prefix = "config")
public class MyConfig {
int value1;
int value2;
String str;
public int getValue1() {
return value1;
}
// Add the rest of getters here...
// Values are already mapped in this class. You can access them via getters.
}
任何想要访问配置值的类
@Import(MyConfig.class)
class MyClass {
private MyConfig myConfig;
@Autowired
public MyClass(MyConfig myConfig) {
this.myConfig = myConfig;
System.out.println( myConfig.getValue1() );
}
}
您可以使用@Value注释并访问您所使用的Spring bean中的属性
@Value("${userBucket.path}")
private String userBucketPath;
Spring Boot文档的Externalized Configuration部分解释了您可能需要的所有细节。
阅读应用程序。属性或应用程序。Yml属性遵循以下步骤:
在应用程序中添加属性。属性或application.yaml 创建配置类并添加属性
application.jwt.secretKey=value
application.jwt.tokenPrefix=value
application.jwt.tokenExpiresAfterDays=value ## 14
application:
jwt:
secret-key: value
token-prefix: value
token-expires-after-days: value ## 14
@Configuration("jwtProperties") // you can leave it empty
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "application.jwt") // prefix is required
public class JwtConfig {
private String secretKey;
private String tokenPrefix;
private int tokenExpiresAfterDays;
// getters and setters
}
注意:在.yaml文件中,你必须使用kabab-case
现在使用配置类实例化它,你可以手动或依赖注入。
public class Target {
private final JwtConfig jwtConfig;
@Autowired
public Target(JwtConfig jwtConfig) {
this.jwtConfig = jwtConfig;
}
// jwtConfig.getSecretKey()
}
为了从属性文件中选择值,我们可以有一个配置读取器类,比如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();
}
另一种方法是将org.springframework.core. environment注入到bean中。
@Autowired
private Environment env;
....
public void method() {
.....
String path = env.getProperty("userBucket.path");
.....
}