我试图设置我的Spring应用程序,以便根据配置文件读取不同的.properties
文件。我用java配置等什么,我试图做的是这样的:如何将环境自动装入PropertySourcesPlaceholderConfigurer?
@Autowired
private static Environment env;
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
String[] profiles = env.getActiveProfiles();
String filestring = "environment."+profiles[0]+".properties";
ClassPathResource properties = new ClassPathResource(filestring);
Resource[] resources = new ClassPathResource[] { properties };
pspc.setLocations(resources);
return pspc;
}
然而env.getActiveProfiles()
是给我一个NullPointerException
,我以为意味着环境尚未注入。任何人有任何想法,我怎么能解决这个问题?或者,如果这是愚蠢的/不可能的我怎么能更好地做到这一点?