mavenプロジェクトでSpringフレームワークを使用して渡された入力に基づいてプロパティファイルを読み込む必要があります。プロパティファイルとアプリケーションコンテキストがsrc/main/resourcesにあります@Autowired Nullポインタ例外を使用してプロパティファイルを読み取る
プロパティファイルを挿入するために環境APIを使用しようとしています。
コード:しかし、私はラインでnullポインタ例外を取得しておく
@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");
}
}
"文字列値= environment.getProperty(国);"
私は次のよう
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");) {
CountryGeoFilter objGeo = (CountryGeoFilter) context.getBean("geoFilter");
GeoFilterStore responseStore = objGeo.getCountryGeoFilter(country);
}
マイapplicationContext.xmlを内の関数を呼び出すしています(SRC /メイン/リソース)
<bean id="geoFilter"
class="com.package.CountryGeoFilter" />
注:私は他のクラスとプロパティを持っています私が同じことをして、beanがapplicationContext.xmlで宣言する必要があるファイル。
私は春には新しく、どこが間違っているのかわからない。どんな助けもありがとう。
は、あなたが正しいです 'CountryGeoFilter' –