我需要根据在 Maven 项目中使用 spring 框架传递的输入来读取属性文件。我的属性文件和应用程序上下文存在于 src/main/resources 下
我正在尝试使用环境 api 来注入属性文件。
代码:
@Component
@Configuration
@PropertySource("classpath:GeoFilter.properties")
public class CountryGeoFilter {
@Autowired
public Environment environment;
public GeoFilterStore getCountryGeoFilter(String country) throws
CountryNotFoundException, IOException {
GeoFilterStore countryFilterStore = new GeoFilterStore();
String value = environment.getProperty(country);
if (value == null) {
throw CountryNotFoundException.getBuilder(country).build();
}
String[] seperateValues = value.split(":");
countryFilterStore.setGameStore(isTrueValue(seperateValues[0]));
countryFilterStore.setVideoStore(isTrueValue(seperateValues[1]));
return countryFilterStore;
}
private boolean isTrueValue(String possibleTrueValue) {
return !possibleTrueValue.equals("No") &&
!possibleTrueValue.equals("N/A");
}
}
但我在“String value = environment.getProperty(country);”行不断收到空指针异常
我正在以下列方式调用该函数
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");) {
CountryGeoFilter objGeo = (CountryGeoFilter) context.getBean("geoFilter");
GeoFilterStore responseStore = objGeo.getCountryGeoFilter(country);
}
我的 applicationContext.xml(src/main/resources)
<bean id="geoFilter"
class="com.package.CountryGeoFilter" />
注意:我有其他类和属性文件,我需要对其执行相同操作,并在 applicationContext.xml 中声明它们的 bean。
我对春天很陌生,不知道我哪里出错了。任何帮助,将不胜感激。