Ben*_*Liu 16 configuration spring properties spring-boot
在我的目标文件夹中,有2个文件夹,lib和conf.所有属性文件都放在conf文件夹中,而jar放在lib foulder中.
在spring boot之前,我们在spring.xml中使用以下配置来使用@value
<context:property-placeholder location="classpath*:*.properties"/>
Run Code Online (Sandbox Code Playgroud)
在java代码中:
@Value("${name}")
private String name;
Run Code Online (Sandbox Code Playgroud)
但在春季启动时,我不知道如何在java代码中做同样的事情.
我试过跟随,但没有工作
@Configuration
@PropertySource(value = "classpath:aaa.properties")
public class AppConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Run Code Online (Sandbox Code Playgroud)
Alb*_*rto 32
我对问题的标题和描述感到有点困惑.希望我不会对你的评论更加困惑.
一般来说,Spring Boot对于项目结构以及创建的二进制文件都非常了解.推荐的方式(Spring Boot意见)是构建一个包含所有依赖关系的jar(胖罐).如果您需要在胖罐外定义的配置属性(或者如果这是您构建的那个,则为war),Spring Boot提供了许多选项(参见参考文献1).我喜欢我的应用程序使用标志(spring.config.location)指向外部文件,该标志可以使用系统属性进行设置:
java -jar -Dspring.config.location=<path-to-file> myBootProject.jar
Run Code Online (Sandbox Code Playgroud)
请注意,您可以通过使用环境变量来定义外部文件所在的位置.
我希望这有帮助!
参考文献:1.https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
小智 12
我不确定你是否处理与我相同的情况,但在我的情况下,我有一个jar和一个*.properties文件.我所做的是让*.properties文件位于jar外面是下一个:
@Configuration
public class ApplicationContext {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("application.properties"));
properties.setIgnoreResourceNotFound(false);
return properties;
}
}
Run Code Online (Sandbox Code Playgroud)
当我设置application.properties文件的位置时,我创建了FileSystemResource对象,这允许我获取位于jar旁边的properties.files.例如,如果.properties文件位于类路径中,则可以使用其他类(如ClassPathResource).您可以阅读spring提供的其他类来获取包org.springframework.core.io下的Resource对象..
我希望这些评论有所帮助.
小智 8
SpringApplication将从
application.properties以下位置的文件加载属性并将它们添加到Spring环境中:
- 当前目录的A/config子目录.
- 当前目录
- 一个classpath/config包
- 类路径根
列表按优先级排序(在列表中较高位置定义的属性将覆盖在较低位置中定义的属性).
一种方法是简单地将'conf'目录重命名为'config',它将毫无问题地工作.因此,除非您希望您的属性文件位于上述4之外的某个位置,否则无需进行额外配置.
在这种情况下,您可以显式定义属性源.
@PropertySource("classpath:config.properties")
Run Code Online (Sandbox Code Playgroud)
和多个属性文件
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:logging.properties"),
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
})
Run Code Online (Sandbox Code Playgroud)
找到了解决办法:
首先创建一个类并添加@ConfigurationProperties
@ConfigurationProperties(prefix = "asdf", locations = "file:conf/aaa.properties")
public class ASDF {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意位置,我使用文件,而不是类路径。
然后在您的应用程序类中,添加 @EnableConfigurationProperties
@SpringBootApplication
@EnableConfigurationProperties({ASDF.class, BBB.class})
public class InitialBeanTestApplication {
public static void main(String[] args) {
SpringApplication.run(InitialBeanTestApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以读取conf文件夹中的配置文件
| 归档时间: |
|
| 查看次数: |
51604 次 |
| 最近记录: |