我是新来的春天框架,我有一些问题,试图读取和使用文件的属性。总结一下,我想要做的是定义一个类,它存储所有读取的属性,第二个类使用这些属性来执行某些操作,第三个类使用结果。春天:使用从文件读取的属性
存储属性的类是:
@Configuration
public class PropertyClass {
@Value("${propertyName")
private Integer propertyName;
@Bean(name = "propertyName")
public Integer getPropertyName() {
return propertyName;
}
}
读取和使用这些属性的类:
@Component
public class PropertyReader {
private Integer myProperty;
@Autowire
@Qualifier("propertyName")
public void setMyProperty(
Integer myProperty) {
this.myProperty = myProperty;
}
public Integer getValue() {
//do something with myProperty
return result;
}
}
和使用PropertyReader类:
public class Utilizer {
private PropertyReader getPropertyReader() {
ApplicationContext context = new AnnotationConfigApplicationContext(PropertyReader.class);
PropertyReader reader = (BakerStorageClassConfigHelper)context.getBean("PropertyReader");
return reader;
}
}
我已将类注册为application-config.xml文件中的bean:
<bean class="property.class.package.PropertyClass" depends-on="Environment" />
<bean class="reader.class.package.PropertyReader" />
而且我有一个environment.xml文件,其中的“环境”bean用位置规则定义以查找属性文件。
现在发生了什么,在类“热利用”当我试图让“ApplicationContext的”对象抛出一个异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PropertyReader':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire method: public void reader.class.package.PropertyReader.setMyProperty(java.lang.Integer);
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.Integer]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我试图改变PropertyReader类的注释@Repository或@Service,并试图添加一个@ComponentScan与指定的PropertyClass包,但没有为我工作..
有人可以给我一些建议吗?
谢谢!
在应该注入的setMyProperty方法中有一个myProperty参数,但事实上它没有在任何地方指定,是吗?也许你可以尝试在PropertyReader bean声明中设置这个属性值。 –
谢谢你的建议格林......我可以问你如何做到这一点? – giocarmine
尝试用以下代替PropertyReader bean声明: –