I need to read properties file based on the input passed using the spring framework in a maven project. My property files and application context are present under src/main/resources
I am trying to use the environment api to inject the properties file.
Code:
@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");
}
}
But i keep getting null pointer exception at line "String value = environment.getProperty(country);"
I am invoking the function in the following manner
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");) {
CountryGeoFilter objGeo = (CountryGeoFilter) context.getBean("geoFilter");
GeoFilterStore responseStore = objGeo.getCountryGeoFilter(country);
}
My applicationContext.xml(src/main/resources)
<bean id="geoFilter"
class="com.package.CountryGeoFilter" />
Note: I have other classes and property files for which i need to do the same and have their beans declared in applicationContext.xml.
I am rather new to spring and not sure where i am going wrong. Any help would be appreciated.