2016-08-01 105 views
0

我是新来的春天框架,我有一些问题,试图读取和使用文件的属性。总结一下,我想要做的是定义一个类,它存储所有读取的属性,第二个类使用这些属性来执行某些操作,第三个类使用结果。春天:使用从文件读取的属性

存储属性的类是:

@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包,但没有为我工作..

有人可以给我一些建议吗?
谢谢!

+0

在应该注入的setMyProperty方法中有一个myProperty参数,但事实上它没有在任何地方指定,是吗?也许你可以尝试在PropertyReader bean声明中设置这个属性值。 –

+0

谢谢你的建议格林......我可以问你如何做到这一点? – giocarmine

+0

尝试用以下代替PropertyReader bean声明:

回答

1

我不明白为什么你需要声明propertyName为整数。 如果您需要的只是从文件中获取属性,那么您可以定义一个PropertiesFactoryBean并将其自动装载到您喜欢的任何其他bean。

假设你有一个包含值的myValues.properties文件:

key1=value1 
key2=value2 

定义豆:

@Bean(name = "myProperties") 
public PropertiesFactoryBean detailQueriesFactoryBean() 
{ 
    PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 
    pfb.setLocation(new ClassPathResource("com/xxx/myValues.properties")); 
    return pfb; 
} 

现在,无论你需要它,这样做:

@Autowired 
@Qualifier("myProperties") 
private Properties myValuesContainer; 

public void myMethod(){ 
    //this will get you "value1" 
    String value1 = myValuesContainer.getProperty("key1"); 
} 

希望这为你工作。

---------------------为你的情况----------------

如果它已经在应用程序上下文中,您可以使用@Value直接在您的PropertyReader中注入值并为它们添加getter/setter。不需要PropertyClass,对吧?

或者您可以将@PostConstruct方法添加到PropertyReader。在该方法内部,您可以从现有上下文中检索所需的值。

@PostContstruct 
public void extractValues(){ 
    //retrieve value from context and assign to whichever var. 
} 
+0

谢谢Leon,事实是,属性文件已经在应用程序的不同位置使用,并且他们被加载在environment.xml文件中设置位置。 我需要读取这些属性的一个子集我定义了“PropertyClass”,并且我希望与其他应用程序的工作保持一致。 – giocarmine

+0

我更新了您的案例的答案。但是我注意到代码中有一件事: ApplicationContext context = new AnnotationConfigApplicationContext(PropertyReader。类); 这看起来不正确。如果您需要一个新的appContext,您通常会将一个'@Configuration'类传递给AnnotationConfigApplicationContext构造函数。 –