我想访问应用程序中提供的值。属性,例如:
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应用程序的主程序中的路径。
当前回答
属性中的@Value("${property-name}") 应用程序。属性 @配置或@组件。
我尝试的另一种方法是用下面的方式创建一个Utility类来读取属性
protected PropertiesUtility () throws IOException {
properties = new Properties();
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("application.properties");
properties.load(inputStream);
}
您可以使用静态方法来获取作为参数传递的键的值。
其他回答
阅读应用程序。属性或应用程序。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()
}
尝试类属性loaderutils ?
这种方法不使用Spring引导的注释。传统的上课方式。
例子:
Resource resource = new ClassPathResource("/application.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String url_server=props.getProperty("server_url");
使用getProperty()方法传递键并访问属性文件中的值。
属性中的@Value("${property-name}") 应用程序。属性 @配置或@组件。
我尝试的另一种方法是用下面的方式创建一个Utility类来读取属性
protected PropertiesUtility () throws IOException {
properties = new Properties();
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("application.properties");
properties.load(inputStream);
}
您可以使用静态方法来获取作为参数传递的键的值。
您可以访问该应用程序。属性文件值使用:
@Value("${key_of_declared_value}")
你可以使用@ConfigurationProperties来访问application.properties中定义的值,这很简单
#datasource
app.datasource.first.jdbc-url=jdbc:mysql://x.x.x.x:3306/ovtools?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
app.datasource.first.username=
app.datasource.first.password=
app.datasource.first.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=8686
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.database=mysql
@Slf4j
@Configuration
public class DataSourceConfig {
@Bean(name = "tracenvDb")
@Primary
@ConfigurationProperties(prefix = "app.datasource.first")
public DataSource mysqlDataSourceanomalie() {
return DataSourceBuilder.create().build();
}
@Bean(name = "JdbcTemplateenv")
public JdbcTemplate jdbcTemplateanomalie(@Qualifier("tracenvDb") DataSource datasourcetracenv) {
return new JdbcTemplate(datasourcetracenv);
}