【发布时间】:2019-07-31 02:22:33
【问题描述】:
大家都知道如果要读取properties文件,可以这样做:
@Configuration
@PropertySource("classpath:/application.properties")
public class AppConfig {
@Value("${app.name}")
public String name;
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public PostService postService() {
return new PostServiceImpl(name);
}
}
但是,现在我有一个类似于 SpringBoot 的框架。它可以将Spring与Mybatis集成。
问题是前面的代码只能读取我的项目类路径文件,但我需要使用我的框架读取属性文件项目。我该怎么做?
更新
对不起大家。可能我没说清楚,所以上图:
我不使用 SpringBoot
我想读取项目(使用我的框架)类路径,而不是我的框架类路径。
谢谢。
【问题讨论】:
-
出了什么问题?
-
这是您想要完成的任务吗? stackoverflow.com/questions/29072628/…
-
@Deadpool 问题是前面的代码只能读取我的项目类路径文件,但我需要使用我的框架读取属性文件项目。我是怎么做到的?
-
@neildo 谢谢。但不是因为我不使用 SpringBoot。
-
如果您要读取的属性文件(无论它来自哪个框架)位于类路径上,您的代码应该可以工作。如果它不在类路径上,您也可以将
@PropertySource与文件位置一起使用:@PropertySource("file:/application.properties")