I'm trying to use @Value to load properties from profile specific properties files under resources dir, but I'm getting "could not resolve propertyPlaceHolder". Below is my code.
@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;
}
}
MainClass
public class Main {
public static void main(String args[]) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
SomeBean bean = context.get("beanName",SomeBean.class);
//logic follows
}
}
I do this because I need to run this as a standalone app.I know something is wrong with this but I don't know what.
With Spring Boot a PropertyPlaceholderConfigurer
is created for you automatically.
If you wish to define a custom location for your properties file(s) you can use the following annotation.
@PropertySource("classpath:foo.properties")
(Just add the annotation on class level to your configuration class, it is also Spring profile aware - different locations for different environments.)
尝试直接在src/main/resources
内部移动dev/application-dev.properties
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.