简体   繁体   中英

How to retrieve the value of a key from properties file in spring using Autowiring

I am new to spring, i have a properties file from which have to read a particular key. I have to use the auto wiring feature.I am giving the code what i have done so far,

<bean id="dnqLtrBatchWorkflow" class="com.twentyfirst.dnqltrbatch.beans.DNQLtrBatchWorkflow" >
    <property name="pldwDataSource" ref="pldwDS" />
    <property name="builder" ref="documentBuilder" />
    <property name="externalLib" value="${pldw.library_name1}"></property> // i want to read this key from the properties file 
</bean>



public class DNQLtrBatchWorkflow extends NonTransactionalAbstractWorkflow<DNQRecord> {

private static final Logger LOGGER = Logger.getLogger(DNQLtrBatchWorkflow.class);
@Autowired
private String externalLib;

 public void aMethod(){

  System.out.println(externalLib); //  i want to print the value here.
  }

 //properties file 
pldw.connection.url=jdbc:as400://OHINDIBMP1:446/TSL50LIB00
pldw.jdbc.username=TSVQTEBAT1
pldw.jdbc.password=LtxQ8jqGcXcfWnGAtot8fw==
pldw.driverClassName=com.ibm.as400.access.AS400JDBCDriver
pldw.library_name1=TSL50LIBIS 

But when i am trying to run this i am getting below exception

 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dnqLtrBatchWorkflow': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.twentyfirst.dnqltrbatch.beans.DNQLtrBatchWorkflow.externalLib; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)

this key pldw.library_name1 i have to get in the class DNQLtrBatchWorkflow.Please help. Thanks in advance

With the Definition of the bean:

<bean id="dnqLtrBatchWorkflow" class="com.twentyfirst.dnqltrbatch.beans.DNQLtrBatchWorkflow" >
    <property name="pldwDataSource" ref="pldwDS" />
    <property name="builder" ref="documentBuilder" />
    <property name="externalLib" value="${pldw.library_name1}"></property> // i want to read this key from the properties file 
</bean>

The setter for the property externalLib is called.

So you have to add the setter for the property and than you can print it out:

public class DNQLtrBatchWorkflow extends NonTransactionalAbstractWorkflow {

private static final Logger LOGGER = Logger.getLogger(DNQLtrBatchWorkflow.class);

private String externalLib;

 public void aMethod(){

  System.out.println(externalLib); //  i want to print the value here.
 }

 public void setExternalLib(String value){
     this.externalLib = value;
 }

The above code should work, if you have configured the PropertyPlaceholderConfigurer correctly

If you want to stay with annotation based class configuration I think what you are looking for is @Value and not @Autowire .

Make sure your property file is on the classpath than annotate your field like this:

@Value("${pldw.library_name1}")
private String externalLib;

This has the benefit that you doesn't even have to write the setter for the field. And remove the property tag from your bean definition.

For more usage of @Value check this: http://www.baeldung.com/spring-value-annotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM