简体   繁体   English

如何在春季使用自动装配从属性文件中检索键的值

[英]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.

相关问题 如何使用 spring 从属性文件中检索值 - How to retrieve values from a properties file using spring 如何使用Spring将嵌套键值对从属性文件加载到Java对象中? - How do I load nested key value pairs from a properties file into a Java object using Spring? 无法使用Spring的@Value,@ ConfigurationProperties注释从Properties文件中检索值 - Unable to retrieve value from Properties file using Spring's @Value, @ConfigurationProperties annotations 如何使用Java从配置文件中检索键值 - How to retrieve the key value from configuration file using java 如何使用.properties文件中的值初始化spring bean中的集合 - How to initialize a set in a spring bean using value from .properties file 使用spring直接将属性自动装配到bean中? - autowiring properties directly into bean using spring? 是否可以在不自动装配被测类的情况下从 application.properties 检索值? - Is it possible to retrieve value from application.properties without autowiring the class under test? 从属性文件JAVA TOMCAT中检索密钥 - Retrieve a Key from a properties file JAVA TOMCAT 如何使用Spring注释将Properties文件中的值注入到现有实例(不受Spring管理)的字段中? - How do I inject a value from Properties file to a field of an existing instance (not managed by Spring) using Spring annotations? 如何在Spring中使用@PropertySource加载的属性文件中配置“动态键” - how to configure 'dynamic key' in properties file loaded using @PropertySource in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM