繁体   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