【发布时间】:2019-08-20 03:16:23
【问题描述】:
我有这个配置需要用于 Spring Boot 应用程序。
server.port=8085
server.servlet.context-path=/authserver
#data source
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=<url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
默认情况下,spring-boot 会选择位于 src/main/resources/ 中的 application.properties 文件
我想更改此路径并将 spring boot 引导到不同的 application.properties 文件
我可以使用
java -jar app.jar --spring.config.location=classpath:/another-location.properties
是否有任何替代解决方案我可以在不通过命令行传递参数的情况下实现这一点?
我在用这个
@PropertySource("file:C:\Users\test\.test\test.properties")
@ConfigurationProperties(prefix = "spring")
public class Configuration {
private String ddlAuto;
private String url;
private String username;
private String password;
private String driverClassName;
}
在我的主课中
@SpringBootApplication
@EnableConfigurationProperties(Configuration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在我尝试执行应用程序后,在 src/main/resources/ 下的 application.properties 中注释掉所有数据源属性 但它一直给我下面提到的错误并且应用程序无法启动
我指的是这个教程:https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/
但正如我提到的,当我启动 spring boot 应用程序时出现此错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException:
对此的任何帮助将不胜感激
【问题讨论】:
-
可能你的进程没有读取文件的权限?你能粘贴完整的堆栈跟踪吗?
-
你能把
@PropertySource注解放在不同的bean上吗?我不确定它是否像这样正确拾取(因为Configuration不会是普通的 bean)。 documentation about externalized configuration 建议将这些放在单独的@Configuration类中。 -
不要在您的代码中嵌入指向外部化文件的路径。这是额外的工作,使实际使用变得更加困难。
-
@Shenali Silva 这可能对你有帮助:callicoder.com/spring-boot-configuration-properties-example
标签: java spring spring-boot