我通过指定WarLauncher(Spring引导加载程序的一部分)作为我的启动类创建了一个可执行war文件.当所有配置文件(属性,弹簧上下文等)都是我的资源文件夹的一部分时,它工作正常.我希望我的战争的消费者需要控制属性文件.因此,它需要在war文件之外加载.我期待配置文件夹中的属性文件(与war文件并排部署).我试图通过使用maven插件向清单添加适当的类路径条目,但它没有奏效.
以下是我的maven POM文件的相关部分的外观:
org.apache.maven.plugins maven-war-plugin 2.3 true org.springframework.boot.loader.WarLauncher < > config/ false
我使用Spring ClassPathResource()来加载属性文件.以下显示了相同的代码段:
InputStream stream = new ClassPathResource(classPathConfigFilePath).getInputStream(); Proerties properties = new Properties(); properties.load(stream);
在运行时,它无法找到导致FileNotFoundException的属性文件.
谢谢.
默认情况下,Spring-Boot会在以下位置搜索application.properties
文件
classpath root
当前目录
classpath /config
包
/config
当前目录的子目录
所有这些文件(如果可用)按该顺序加载,这意味着1的属性可以被2,3,4覆盖.所有已加载的属性都可作为其中的一部分Environment
使用,因此可用于占位符以进行配置.
作为上述加载规则的补充,还可以加载配置文件特定文件.对于给定的配置文件,它也会尝试加载application-{profile}.properties
.对于该特定文件,还考虑了如上所述的加载规则.
所有已加载的属性都可以Environment
通过弹簧统一属性管理来获得.可以使用Environment
直接检索配置参数,也可以使用带有@Value
注释的占位符进行配置
@Configuration public class SomeConfigClass { @Autowired private Environment env; public DataSource dataSource() { SimpleDriverDataSource ds = new SimpleDriverDataSource(); ds.setUsername(env.getProperty("jdbc.username")); ds.setPassword(env.getProperty("jdbc.password")); ds.setDriverClass(Driver.class); ds.setUrl(env.getProperty("jdbc.url")); return ds; } }
或者 @Value
@Configuration public class SomeConfigClass { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Value("${jdbc.url}") private String url public DataSource dataSource() { SimpleDriverDataSource ds = new SimpleDriverDataSource(); ds.setUsername(username); ds.setPassword(password); ds.setDriverClass(Driver.class); ds.setUrl(url); return ds; } }
链接
Spring boot READ-ME
Spring Framework Profile 文档
Spring Property管理博客
Spring Boot Loader READ-ME