Constructor injection is desirable for imutability but the Spring config is very much tied to implementation (ordering of args).
We could solve this problem using a @Property("..") annotation to decorate a constructor with the names of the ctor paramaters? (could locate param names from CGLIB but only if debug symbols stored in class).
For example:
public class Person
{
public Person( @Property("age") int age, @Property("name") String name )
{
...
}
}
Then the config could look same as the if we had used setter injection.
This could be achieved using a BeanFactoryPostProcessor to visit every bean definition and move properties from the setter map to the constructor map respecting the order the annotations are definied in?
Alternativly we could add an attribute to constructor-arg
Constructor injection is desirable for imutability but the Spring config is very much tied to implementation (ordering of args).
We could solve this problem using a @Property("..") annotation to decorate a constructor with the names of the ctor paramaters? (could locate param names from CGLIB but only if debug symbols stored in class).
For example:
public class Person
{
public Person( @Property("age") int age, @Property("name") String name )
{
...
}
}
Then the config could look same as the if we had used setter injection.
<bean class="Person">
<property name="age" value=".."/>
<property name="name" value=".."/>
</bean>
This could be achieved using a BeanFactoryPostProcessor to visit every bean definition and move properties from the setter map to the constructor map respecting the order the annotations are definied in?
Alternativly we could add an attribute to constructor-arg
<bean class="Person">
<constructor-arg name="age" value=".."/>
<constructor-arg name="name" value=".."/>
</bean>
Dave Syer added a comment - 29/Mar/07 08:30 AM The constructor naming requirement is possibly related or duplicate of SPR-2077 (and SPR-1887?), but not enough detail there to tell.
Eugene Kuleshov added a comment - 29/Mar/07 11:22 AM Please note that Java 6 has java.beans.ConstructorProperties annotation, which is used like this:
{code}
public class Point {
@ConstructorProperties({"x", "y"})
public Point(int x, int y) {
this.x = x;
this.y = y;
}
....
{code}
SPR-2077(and SPR-1887?), but not enough detail there to tell.