Java 无法使用Spring Boot加载特定于配置文件的属性文件
我试图使用@Value从resources dir下的配置文件特定属性文件加载属性,但得到的结果是“无法解析propertyPlaceHolder”。下面是我的代码Java 无法使用Spring Boot加载特定于配置文件的属性文件,java,spring,spring-boot,properties-file,spring-profiles,Java,Spring,Spring Boot,Properties File,Spring Profiles,我试图使用@Value从resources dir下的配置文件特定属性文件加载属性,但得到的结果是“无法解析propertyPlaceHolder”。下面是我的代码 @ComponentScan(basePackages="com.my.package") @Import({DevConfig.class,QAConfig.class} @SpringBootApplicaton() public class Config(){ @Value("${someProperty"}) p
@ComponentScan(basePackages="com.my.package")
@Import({DevConfig.class,QAConfig.class}
@SpringBootApplicaton()
public class Config(){
@Value("${someProperty"})
private String property;
@Bean
public void getProperty(){
return property;
}
//other beans
}
DevConfig.java
@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[] { new ClassPathResource("dev/application-dev.properties") };
ppc.setLocations(resources);
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
主类
public class Main {
public static void main(String args[]) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
SomeBean bean = context.get("beanName",SomeBean.class);
//logic follows
}
}
我这样做是因为我需要将其作为一个独立的应用程序运行。我知道这有问题,但我不知道是什么。尝试将
dev/application-dev.properties
直接移动到src/main/resources
尝试将dev/application-dev.properties
直接移动到src/main/resources
中Spring Boot会自动为您创建一个PropertyPlaceHolderConfigure
如果要为属性文件定义自定义位置,可以使用以下注释
@PropertySource("classpath:foo.properties")
(只需将类级别的注释添加到您的配置类中,它也支持Spring概要文件-不同环境的不同位置。)使用Spring Boot,会自动为您创建一个
属性PlaceHolderConfigure
如果要为属性文件定义自定义位置,可以使用以下注释
@PropertySource("classpath:foo.properties")
(只需将类级别的注释添加到您的配置类中,它也支持Spring概要文件-不同环境的不同位置。)