使用构造函数-arg字段填充spring bean [英] Populating a spring bean using a constructor-arg field

查看:196
本文介绍了使用构造函数-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>

这利用了使用任意静态工厂方法创建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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆