本文介绍了使用构造函数-arg字段填充spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用该字段注入包含Map的属性文件作为附加构造函数arg。

How can i inject a properties file containing a Map to be used as additional constructor arg using the field.

从属性文件加载Map

With a Map being loaded from a properties file

目前使用以下方式设置bean:

the bean is currently setup using:

<bean id="graphDbService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
     init-method="enableRemoteShell" destroy-method="shutdown">

     <constructor-arg index="0" value= "data/neo4j-db"/>
         <constructor-arg index="1" value=?  />
</bean>

Java等价物:

Map<String,String> configuration =  EmbeddedGraphDatabase.loadConfigurations( "neo4j_config.props" );
GraphDatabaseService graphDb = new EmbeddedGraphDatabase( "data/neo4j-db", configuration );

谢谢

推荐答案

这样的事情:

<bean id="configuration" class="org.neo4j.kernel.EmbeddedGraphDatabase"
      factory-method="loadConfigurations">
   <constructor-arg value="neo4j_config.props"/>
</bean>

<bean id="graphDbService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
     init-method="enableRemoteShell" destroy-method="shutdown">

     <constructor-arg index="0" value="data/neo4j-db"/>
     <constructor-arg index="1" ref="configuration"  />
</bean>

这利用了 ,在这种情况下使用 loadConfigurations()作为工厂方法来创建配置 bean,然后将其注入适当的构造函数 EmbeddedGraphDatabase

This takes advantage of the ability to create beans using arbitrary static factory methods, in this case using loadConfigurations() as a factory method to create the configuration bean, which is then injected into the proper constructor of EmbeddedGraphDatabase.

这篇关于使用构造函数-arg字段填充spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:11