使用@Autowired Environment spring java空指针异常读取属性文件
我需要根据在maven项目中使用spring框架传递的输入读取属性文件。我的属性文件和应用程序上下文位于src/main/resources下使用@Autowired Environment spring java空指针异常读取属性文件,java,spring,maven,autowired,properties-file,Java,Spring,Maven,Autowired,Properties File,我需要根据在maven项目中使用spring框架传递的输入读取属性文件。我的属性文件和应用程序上下文位于src/main/resources下 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean class="org.springframework.beans.factory.config.Propert
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:GeoFilter.properties" />
</bean>
我正在尝试使用环境api注入属性文件
代码:
@Component
@Configuration
@PropertySource("classpath:GeoFilter.properties")
public class CountryGeoFilter {
@Autowired
public Environment environment;
@Bean
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);”行中得到空指针异常
My applicationContext.xml(src/main/resources)
我对春天很陌生,不知道我会错在哪里。任何帮助都将不胜感激
编辑:
我更新了启动代码以使用上下文
ApplicationContext context = new
AnnotationConfigApplicationContext(CountryGeoFilter.class);
CountryGeoFilter testGeoFilter =
context.getBean(CountryGeoFilter.class);
testGeoFilter.getCountryGeoFilter(country);
现在我得到以下异常
Exception in thread "main"
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'getCountryGeoFilter' defined in ..
Unsatisfied dependency expressed through method
'getCountryGeoFilter' parameter
0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'java.lang.String' available: expected at
least 1 bean which qualifies as autowire candidate. Dependency
annotations: {}
我在下面添加了一个基本的工作解决方案。本质上,删除
getCountryGeoFilter()
方法上的@Bean,并更改调用它的方式。我在@RestController类中提供了一个示例调用
此外,此配置没有使用任何XML配置
TestClass.java
测试属性
TestController.java
我在下面添加了一个基本的工作解决方案。本质上,删除
getCountryGeoFilter()
方法上的@Bean,并更改调用它的方式。我在@RestController类中提供了一个示例调用
此外,此配置没有使用任何XML配置
TestClass.java
测试属性
TestController.java
工作得很好。只需删除@Bean注释。谢天谢地。只需删除@Bean注释。谢谢
ApplicationContext context = new
AnnotationConfigApplicationContext(CountryGeoFilter.class);
CountryGeoFilter testGeoFilter =
context.getBean(CountryGeoFilter.class);
testGeoFilter.getCountryGeoFilter(country);
Exception in thread "main"
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'getCountryGeoFilter' defined in ..
Unsatisfied dependency expressed through method
'getCountryGeoFilter' parameter
0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'java.lang.String' available: expected at
least 1 bean which qualifies as autowire candidate. Dependency
annotations: {}
@Component
@Configuration
@PropertySource("classpath:test.properties")
public class TestClass {
@Autowired
Environment environment;
public String test (String property) {
final String value = environment.getProperty(property);
System.out.println("========> Property: " + value);
// TODO: Something with the prop val
return value;
}
}
prop1=prop_1_value
prop2=prop_2_value
@RestController
@RequestMapping("/test")
public class TestController {
private TestClass testClass;
TestController(TestClass testClass) {
this.testClass = testClass;
}
@RequestMapping("/{propName}")
public String test(@PathVariable String propName) {
return testClass.test(propName);
}
}