[英]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. 这个键pldw.library_name1我必须进入DNQLtrBatchWorkflow类。请帮助。 Thanks in advance
提前致谢
With the Definition of the bean: 用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. 属性
externalLib
的设置器被调用。
So you have to add the setter for the property and than you can print it out: 因此,您必须为属性添加setter,然后才能将其打印出来:
public class DNQLtrBatchWorkflow extends NonTransactionalAbstractWorkflow { 公共类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;
}
The above code should work, if you have configured the PropertyPlaceholderConfigurer
correctly 如果正确配置了
PropertyPlaceholderConfigurer
,则上面的代码应该起作用
If you want to stay with annotation based class configuration I think what you are looking for is @Value and not @Autowire . 如果您想保留基于注释的类配置,我想您要找的是@Value而不是@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. 这样的好处是,您甚至不必为该字段编写setter。 And remove the property tag from your bean definition.
并从bean定义中删除属性标签。
For more usage of @Value check this: http://www.baeldung.com/spring-value-annotation 有关@Value的更多用法, 请检查: http : //www.baeldung.com/spring-value-annotation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.