
Originally Posted by
bas-i
Hi,
I'm sort of beginning to see the light

Great
I`m using Spring for more than a year and I still see new lights. I do a lot of multithreading in my systems and since Spring I have seen a complete street with megawatt lighttowers in them.
And the best of this all is that my codebase is influenced by ideas from Spring (and a lot of new insights on my side), but isn`t dependant on Spring. I could use it in another IOC container or can wire the objects in plain old java.
When doing something new I usually want to have a balance with how far to take things. One small step at a time.
This is my preferred style too. If you don`t have a good foundation, you have nothing at all. First small steps and then bigger steps.
When you move away from the original way of implementing classes that read their own configuration (which possibly is bad but still a fact of life in our codebase) and have them injected, you're pretty much committing to do it like that everywhere from now on (having the client read the config and call 5 setters on the implementing class does *not* sound like a good idea). Hm. Did I mention I like small steps?
If these arguments are very important, I prefer the constructor injection (you don`t have to worry about setters if you don`t have them).
And having a static factory method in your class isn`t that bad, if you still have access to the constructor that accepts the 'base' arguments.
bad:
Code:
class Foo{
String a;
String b;
public Foo(String propertyfile){
Properties props = new Properties(propertyfile);
a = props.get("a");
b = props.get("b");
}
}
good:
Code:
class Foo{
public static Foo createFromPropertyfile(String propertyFile){
Properties props = new Properties(propertyfile);
return new Foo(props.get("a"),props.get("b"));
}
String a,b;
public Foo(String a, String b){
this.b = a;
this.b = b;
}
}
And you can call this in Spring:
Code:
<bean id="foo"
class="Foo"
factory-method="createFromPropertyfile">
<constructor-arg value="foo.properties"/>
</bean>
So having a factory method isn`t that bad, if you still have access to the widest constructor.