簡體   English   中英

如何在春季使用自動裝配從屬性文件中檢索鍵的值

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

我是春季新手,我有一個屬性文件,必須從中讀取特定的鍵。 我必須使用自動接線功能。我要提供到目前為止我所做的代碼,

<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 

但是當我嘗試運行此程序時,我得到了例外

 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)

這個鍵pldw.library_name1我必須進入DNQLtrBatchWorkflow類。請幫助。 提前致謝

用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>

屬性externalLib的設置器被調用。

因此,您必須為屬性添加setter,然后才能將其打印出來:

公共類DNQLtrBatchWorkflow擴展了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;
 }

如果正確配置了PropertyPlaceholderConfigurer ,則上面的代碼應該起作用

如果您想保留基於注釋的類配置,我想您要找的是@Value而不是@Autowire

確保您的屬性文件在類路徑上,而不是像這樣注釋您的字段:

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

這樣的好處是,您甚至不必為該字段編寫setter。 並從bean定義中刪除屬性標簽。

有關@Value的更多用法, 檢查: http : //www.baeldung.com/spring-value-annotation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM