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

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

问题描述

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

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

从属性文件加载地图

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() 作为创建 configuration 的工厂方法 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.

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

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