如何使用自动装配

时间:2017-08-22 14:35:11

标签: java spring

我是春天的新手,我有一个属性文件,必须从中读取特定的密钥。我必须使用自动接线功能。我正在给出我到目前为止所做的代码,

<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.Please帮助。提前致谢

2 个答案:

答案 0 :(得分:1)

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

所以你必须为属性添加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

,则上述代码应该有效

答案 1 :(得分:1)

如果你想继续使用基于注释的课程配置,我认为你要找的是 @Value ,而不是 @Autowire

确保您的属性文件位于类路径上,而不是像这样注释您的字段:

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

这样做的好处是您甚至不必为该字段编写setter。并从bean定义中删除属性标记。

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