|
From: Colin S. <col...@ex...> - 2003-11-09 22:28:24
|
jürgen höller [werk3AT] wrote: >Everybody, > >I'm pleased to announce that a Spring bean factory is now capable of handling Type 3 IoC components! As the most important feature, I've introduced autowire="constructor" for autowiring constructor arguments by type, just like autowire="byType" does for bean properties. > >Furthermore, there is a "constructor-arg" tag within the "bean" tag now, to pass specific beans or values to constructor arguments. "constructor-arg" can specify generic values that get matched by type, or values for a specific argument via its "index" attribute. This means that we can handle any kind of constructor, be it with bean references, simple values, lists, maps, etc. We can also easily handle multiple constructor arguments of the same type, i.e. 2 DataSource arguments or 2 String values (in contrast to the current Pico version)! > > Good stuff. Aside from the marketing, even if you prefer the no-arg constructor, javabeans style, if you're working with existing classes sometimes you have no choice. > >The only limitation is that constructor-wired components are just allowed to have one single constructor. This is also what Pico recommends, although they have some heuristic kind of constructor choice in case of multiple constructors now. I guess we can limit this to one single constructor for the time being. Of course, standard beans defined without autowire="constructor" or "constructor-arg" tags can still have any number of constructors, the only requirement being that they must provide a no-arg constructor. > > One way you could do it (which you probably thought of) is by allowing the deployer to specify, in the case of the indexed variant, an optional 'type' attribute which specifies the real type (in the receiving object) of that constructor argument. This would allow the appropriate constructor to be picked without any ambiguity. I don't know if this is worth doing or not, but on the other hand, if you are going to support constructors at all, it's somewhat arbitrary to stop at 1 if you can figure out a relatively simple (albeit wordy) way to support the multiple case. Even if that is not done, maybe it is worth it supporting the special case of classes which have a no-arg constructor, and one other constructor with one or more args. That's also a pretty common case, and easy to support. Regards, Colin |