Java 使用构造函数参数字段填充 spring bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3466437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populating a spring bean using a constructor-arg field
提问by patrick-fitzgerald
How can i inject a properties file containing a Map to be used as additional constructor arg using the field.
如何使用该字段注入包含 Map 的属性文件以用作附加构造函数 arg。
With a Map being loaded from a properties file
从属性文件加载地图
the bean is currently setup using:
目前使用以下方法设置 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" value=? />
</bean>
Java Equivalent:
Java 等效项:
Map<String,String> configuration = EmbeddedGraphDatabase.loadConfigurations( "neo4j_config.props" );
GraphDatabaseService graphDb = new EmbeddedGraphDatabase( "data/neo4j-db", configuration );
Thanks
谢谢
采纳答案by skaffman
Something like this:
像这样的东西:
<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>
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
.
这利用了使用任意静态工厂方法创建 bean 的能力,在这种情况下使用loadConfigurations()
作为工厂方法来创建configuration
bean,然后将 bean 注入到适当的构造函数中EmbeddedGraphDatabase
。
回答by Aaron Digulla
Create a bean that loads the properties (and takes the file name as an argument) and inject that instead.
创建一个加载属性的 bean(并将文件名作为参数)并注入它。
EDITWhen using annotations, things like constructor injection become more simple:
编辑使用注释时,构造函数注入之类的事情变得更加简单:
@Bean
public Map<String,String> configuration() {
return EmbeddedGraphDatabase.loadConfigurations( "neo4j_config.props" );
}
@Bean
public GraphDatabaseService graphDb() {
return new EmbeddedGraphDatabase( "data/neo4j-db", configuration() );
}
Note that the second bean definition method "simply" calls the first. When this code is executed, Spring will do some magic so you can still override the bean elsewhere (i.e. beans still overwrite each other) and it will make sure that the method body will be executed only once (no matter how often and from where it was called).
请注意,第二个 bean 定义方法“简单地”调用了第一个。当这段代码被执行时,Spring 会做一些魔法,所以你仍然可以在其他地方覆盖这个 bean(即 bean 仍然相互覆盖),并且它将确保方法体只执行一次(无论它多久执行一次,从哪里开始)被称为)。
If the config is in a different @Configuration
class, then you can @Autowired
it:
如果配置在不同的@Configuration
类中,那么你可以@Autowired
:
@Autowired
private Map<String,String> configuration;
@Bean
public GraphDatabaseService graphDb() {
return new EmbeddedGraphDatabase( "data/neo4j-db", configuration );
}