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.
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. 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: 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: Now I can define my specific car, taking all the default values by referencing the bean where they are defined via parent: 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: 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: 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: You can also do this more concisely by using nested properties: 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: 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. 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.
This looks promising, although I will have to add properties to my beans - I'm presently using constructor injection. I read that spring supports nested properties syntax. Can a child bean definition override the parent using nested properties, e.g.. Then I don't need to know about what bean is already instantiated when I override.
– mdma May 21 '10 at 9:11 constructor injection is pretty much the worst way to go with spring imho. – Justin May 21 '10 at 15:04 @Justin - couldn't agree more - using constructors places so many restrictions, it's hard to read and don't work on a friday. I'll be glad to change to property injection.
The original code predates spring and spring was grafted into it. At the time spring was new and there was no clear advice which style of injection to choose, so we stuck with c'tor injection, but over time, advice sided more and more with properties injection. – mdma May 21 '10 at 18:11 @mdma - yes, you can use nested properties, which I didn't know about until you asked :-) – SingleShot May 21 '10 at 23:41 @Singleshot - That's good news.
I can't remember where I read about the nested properties, and didn't find it in the spring reference when I searched. It would really help if you could post a link and extend your sample to include a nested property override. – mdma May 21 '10 at 23:50.
Your example will not work as specified, because the nested bean definition has no class or parent specified. You'd need to add more information, like this: Although I'm not sure if it's valid to refer to nested beans by name like that. Nested bean definitions should be treated with caution; they can quickly escalate into unreadability.
Consider defining the inner beans as top-level beans instead, which would make the outer bean definitions easier to read. As for the child beans needing to know the constructor index of the parent bean, that's a more basic problem with Java constructor injection, in that Java constructor arguments cannot be referred to by name, only index. Setter injection is almost always more readable, at the cost of losing the finality of constructor injection.
A custom schema is always an option, as you mentioned, although it's a bit of a pain to set up. If you find yourself using this pattern a lot, it might be worth the effort.
You may want to look at ApplicationContextAware, and use context scanning to build up your handler chain (in your bean init method), rather than building it up through constructor args. – Justin May 19 '10 at 17:19 Would that work if all the handlers implement the same interface? – mdma May 20 '10 at 23:23.
You can config beans to have a factory and you could encode the varying parameters in the factory creation...
To expand on the factory pattern from Patrick: you can use a prototype bean to get pre-wired dependencies: ... Now, this works best if you use setter injection (rather than constructor arguments), i'm not sure you can even do it you require constructor args. Public class PrototypeConsumingBean implements ApplicationContextAware { public void dynmicallyCreateService(String serviceParam) { // creates a new instance because scope="prototype" MyService newServiceInstance = (MyService)springContext. GetBean("protoBean"); newServiceInstance.
SetParam(serviceParam); newServiceInstance.mySetup(); myServices. Add(newServiceInstance); } public void setApplicationContext(ApplicationContext ctx) { m_springContext = ctx; } }.
I considered using a factory, and I'm ambivilent - I am torn between wanting to keep the spring config simple and keeping construction/configuration out of my code. Using a factory will push a big chunk of the wiring back into the code. – mdma May 20 '10 at 23:29 Yea, the factory pattern only makes sense if you are creating your complex beans dynamically.
If you can do everything through spring the above pattern doesn't pay off; it wasn't exactly clear from the original question what you required. – Justin May 21 '10 at 7:10.
Your example will not work as specified, because the nested bean definition has no class or parent specified. You'd need to add more information, like this.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.