gpt4 book ai didi

java - 使用@Autowired环境读取属性文件 spring java 空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:32 25 4
gpt4 key购买 nike

我需要根据在 Maven 项目中使用 Spring 框架传递的输入来读取属性文件。我的属性文件和应用程序上下文位于 src/main/resources 下

我正在尝试使用环境 api 来注入(inject)属性文件。

代码:

@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);”行不断收到空指针异常

我的applicationContext.xml(src/main/resources)

<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:GeoFilter.properties" />
</bean>

我还在 web.xml 中设置了 contextparam

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

我通过以下方式启动并调用该函数

    CountryGeoFilter objGeo = new CountryGeoFilter();
GeoFilterStore response = objGeo.getCountryGeoFilter(country);
return response;

我对 Spring 很陌生,不知道我哪里出错了。任何帮助将不胜感激。

编辑:

我更新了启动代码以使用上下文

    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

@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

TestController.java

@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);
}

}

关于java - 使用@Autowired环境读取属性文件 spring java 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45494638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com