问题描述
我已经在,但我对可能的和不可能的事感到困惑。
I've read the Spring 3 reference on inheriting bean definitions, but I'm confused about what is possible and not possible.
例如,一个带有协作者bean的bean,配置值为12
For example, a bean that takes a collaborator bean, configured with the value 12
<bean name="beanService12" class="SomeSevice">
<constructor-arg index="0" ref="serviceCollaborator1"/>
</bean>
<bean name="serviceCollaborator1" class="SomeCollaborator">
<constructor-arg index="0" value="12"/>
<!-- more cargs, more beans, more flavor -->
</bean>
然后,我希望能够创建类似的bean,配置略有不同的协作者。我可以做类似
I'd then like to be able to create similar beans, with slightly different configured collaborators. Can I do something like
<bean name="beanService13" parent="beanService12">
<constructor-arg index="0">
<bean>
<constructor-arg index="0" value="13"/>
</bean>
</constructor>
</bean>
我不确定这是否可能,如果是的话,感觉有点笨重。是否有更好的方法来覆盖大型嵌套bean定义的小部分?孩子豆似乎必须对父母有很多了解,例如构造函数索引。
I'm not sure this is possible and, if it were, it feels a bit clunky. Is there a nicer way to override small parts of a large nested bean definition? It seems the child bean has to know quite a lot about the parent, e.g. constructor index.
这是一个玩具示例 - 实际上,服务是依赖于许多其他协作者bean的大型bean定义,它们还具有其他bean依赖性。例如,创建了一系列处理程序,每个bean引用链中的下一个,引用下一个。我想创建一个几乎完全相同的链,中间的处理程序有一些小改动,我该怎么做?
This is a toy example - in practice the service is a large bean definition relying on many other collaborator beans, which have also other bean dependencies. For example, a chain of handlers were created with each bean referencing the next in the chain, which references the next. I want to create an almost identical chain with some small changes to handlers in the middle, how do I it?
我不想改变结构 - 服务bean使用协作者来执行他们的功能,但是如果有帮助我可以添加属性并使用属性注入。
I'd prefer not to change the structure - the service beans use collaborators to perform their function, but I can add properties and use property injection if that helps.
这是一个重复的模式,是否会创建自定义模式帮助?
This is a repeated pattern, would creating a custom schema help?
感谢您的建议!
编辑:我的问题的核心是,如果我有一个非常大的bean定义,正在创建一个复杂的bean的bean(bean具有bean等特性),我想创建一个几乎相同的bean,但有一些变化,我该怎么做?请提一下你的解决方案是否必须使用适当的,或者是否可以使用构造函数注入。
The nub of my question is, if I have a really large bean definition, with a complex hiearchy of beans being created (bean having properites that are bean etc.), and I want to create a bean that is almost the same with a few changes, how to I do it? Please mention if your solution has to use properites, or if constructor injection can be used.
嵌套与顶级bean不是问题(事实上,我认为所有豆类在实践中都处于顶级水平。)
Nested vs. top-level beans are not the issue (in fact, I think all the beans are top level in practice.)
EDIT2:到目前为止,感谢您的答案。 FactoryBean可能就是答案,因为这会降低spring上下文的复杂性,并允许我仅将差异指定为工厂的参数。但是,将一大块上下文推回到代码中并不合适。我听说弹簧可以用于脚本,例如groovy - 这提供了另一种选择吗?工厂可以在groovy中创建吗?
Thank you for your answers so far. A FactoryBean might be the answer, since that will reduce the complexity of the spring context, and allow me to specify just the differences as parameters to the factory. But, pushing a chunk of context back into code doesn't feel right. I've heard that spring can be used with scripts, e.g. groovy - does that provide an alternative? Could the factory be created in groovy?
推荐答案
我不完全确定你想要实现的目标。我不认为你可以在不创建自己的自定义模式的情况下完全实现你想要的东西(这对于嵌套结构来说是非常重要的),但是下面的例子可能非常接近而没有这样做。
I'm not entirely sure what you are trying to achieve. I don't think you can achieve exactly what you want without creating your own custom schema (which is non-trivial for nested structures), but the following example is probably pretty close without doing that.
首先,定义一个抽象bean作为外部bean的模板(我的例子使用Car作为外部bean,Engine作为内部bean),给它所有其他bean都可以继承的默认值:
First, define an abstract bean to use as a template for your outer bean (my example uses a Car as the outer bean and an Engine as the inner bean), giving it default values that all other beans can inherit:
<bean id="defaultCar" class="Car" abstract="true">
<property name="make" value="Honda"/>
<property name="model" value="Civic"/>
<property name="color" value="Green"/>
<property name="numberOfWheels" value="4"/>
<property name="engine" ref="defaultEngine"/>
</bean>
由于所有本田思域都有相同的引擎(在我的世界里,我对汽车一无所知),我给它一个默认的嵌套引擎bean。不幸的是,bean无法引用抽象bean,因此默认引擎不能是抽象的。我已经为引擎定义了一个具体的bean,但是将它标记为 lazy-init
所以它实际上不会被实例化,除非另一个bean使用它:
Since all Honda Civics have the same engine (in my world, where I know nothing about cars), I give it a default nested engine bean. Unfortunately, a bean cannot reference an abstract bean, so the default engine cannot be abstract. I've defined a concrete bean for the engine, but mark it as lazy-init
so it will not actually be instantiated unless another bean uses it:
<bean id="defaultEngine" class="Engine" lazy-init="true">
<property name="numberOfCylinders" value="4"/>
<property name="volume" value="400"/>
<property name="weight" value="475"/>
</bean>
现在我可以定义我的特定汽车,通过引用定义它们的bean来获取所有默认值通过 parent
:
Now I can define my specific car, taking all the default values by referencing the bean where they are defined via parent
:
<bean id="myCar" parent="defaultCar"/>
我的妻子有一辆和我一样的车,除了它的不同型号(再一次,我一无所知)汽车 - 让我们假设发动机是相同的,即使在现实生活中它们可能不是)。我没有重新定义一堆bean /属性,而是再次扩展默认汽车定义,但覆盖其中一个属性:
My wife has a car just like mine, except its a different model (again, I know nothing about cars - let's assume the engines are the same even though in real life they probably are not). Instead of redefining a bunch of beans/properties, I just extend the default car definition again, but override one of its properties:
<bean id="myWifesCar" parent="defaultCar">
<property name="model" value="Odyssey"/>
</bean>
我的姐姐与我的妻子(真的)有同一辆车,但它有不同的颜色。我可以扩展一个具体的bean并覆盖它上面的一个或多个属性:
My sister has the same car as my wife (really), but it has a different color. I can extend a concrete bean and override one or more properties on it:
<bean id="mySistersCar" parent="myWifesCar">
<property name="color" value="Silver"/>
</bean>
如果我喜欢赛车迷你车,我可能会考虑购买更大的发动机。在这里,我扩展了一个小型货车豆,用新引擎覆盖它的默认引擎。这个新引擎扩展了默认引擎,覆盖了一些属性:
If I liked racing minivans, I might consider getting one with a bigger engine. Here I extend a minivan bean, overriding its default engine with a new engine. This new engine extends the default engine, overriding a few properties:
<bean id="supedUpMiniVan" parent="myWifesCar">
<property name="engine">
<bean parent="defaultEngine">
<property name="volume" value="600"/>
<property name="weight" value="750"/>
</bean>
</property>
</bean>
您还可以使用:
<bean id="supedUpMiniVan" parent="myWifesCar">
<property name="engine.volume" value="600"/>
<property name="engine.weight" value="750"/>
</bean>
这将使用defaultEngine。但是,如果您以这种方式创建两辆汽车,每辆汽车都具有不同的属性值,则行为将不正确。这是因为两辆车将共享相同的引擎实例,第二辆车将覆盖第一辆车上设置的属性设置。这可以通过将defaultEngine标记为原型来解决,每次引用它时都会实例化一个新原型:
This will use the "defaultEngine". However, if you were to create two cars this way, each with different property values, the behavior will not be correct. This is because the two cars would be sharing the same engine instance, with the second car overriding the property settings set on the first car. This can be remedied by marking the defaultEngine as a "prototype", which instantiates a new one each time it is referenced:
<bean id="defaultEngine" class="Engine" scope="prototype">
<property name="numberOfCylinders" value="4"/>
<property name="volume" value="400"/>
<property name="weight" value="475"/>
</bean>
我认为这个例子给出了基本的想法。如果您的数据结构很复杂,您可以定义多个抽象bean,或者创建几个不同的抽象层次结构 - 特别是如果您的bean层次结构比两个bean更深。
I think this example gives the basic idea. If your data structure is complex, you might define multiple abstract beans, or create several different abstract hierarchies - especially if your bean hierarchy is deeper than two beans.
附注:我的例子使用了属性,我相信在Spring xml和Java代码中都更清楚。但是,完全相同的技术适用于构造函数,工厂方法等。
Side note: my example uses properties, which I believe are much clearer to understand, both in Spring xml and in Java code. However, the exact same technique works for constructors, factory methods, etc.
这篇关于spring - 构造函数注入和覆盖嵌套bean的父定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!