弹簧启动如何选择外部化的弹簧属性文件



我有这个配置,需要用于弹簧引导应用程序。

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 引导到不同的应用程序属性文件

我可以使用

java -jar app.jar --spring.config.location=classpath:/another-location.properties  

有没有其他解决方案可以在不通过命令行传递 args 的情况下实现这一点?

我正在使用这个

@PropertySource("file:C:Userstest.testtest.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/下的应用程序属性后,在那里 但是它一直给我下面提到的错误,应用程序无法启动

我指的是本教程:https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/

但如前所述,当我启动 Spring 启动应用程序时出现此错误

***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException: 

任何这方面的帮助将不胜感激

具有外部化属性的推荐方法是使用 spring.config.location 系统属性,方法是像这样启动应用程序:

java -jar -Dspring.config.location=/path/to/my/file.properties app.jar

这样做的原因是您没有在代码和文件系统层次结构之间添加耦合。

在 Spring Boot 2.0 之前,此属性是累加的,这意味着它将补充默认位置。在 Spring Boot 2.0 之后,spring.config.location 会替换默认位置(例如 classpath src/main/resources/application.properties)。要在 2.0 之后保持加法行为,请改用 spring.config.additional-location。

有关此问题的官方文档,请参阅此处。

我能够让它在Spring Boot 2.1.2.RELEASE上正常工作。这是我所做的: 我的/tmp文件夹中有一个包含以下内容的test.properties

test.myprop=hello

我在资源文件夹中也有通常的属性文件:

myprop=world

我已经为自定义属性文件创建了一个类:

@Configuration
@PropertySource("file:/tmp/test.properties")
@ConfigurationProperties(prefix = "test")
public class TestConfig {
private String myprop;
public String getMyprop() {
return myprop;
}
public void setMyprop(String myprop) {
this.myprop = myprop;
}
}

然后在我的主类中,我启用了配置属性:

@EnableConfigurationProperties(TestConfig.class)
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}

现在我有了这个测试控制器:

@RestController
public class TestController {
@Value("${test.myprop}")
private String externalisedProp;
@Value("${myprop}")
private String prop;
@GetMapping("test")
public void test() {
System.out.println("externalised: " + externalisedProp);
System.out.println("not externalised" + prop);
}
}

一旦调用,就可以正确打印:

externalised: hello
not externalised: world

我的TestConfig类与MyApp主类位于同一包中。 我所做的与您的解决方案非常相似,几乎相同,您确定您的路径是正确的吗?另外,我可以看到您的属性文件的内容与您在配置类中的内容不匹配,前缀不同。也许这就是问题所在?

编辑: 我试图从我的属性类中删除@Configuration注释(您也没有),但它无法再拾取外部化属性。虽然错误是不同的,但您应该尝试添加它。

最新更新