I have a builder class defined as below in a 3rd party library:
public class KeyBuilder {
private A a;
private B b;
private C c;
public KeyBuilder withA(final A a){
this.a = a;
return this;
}
public KeyBuilder withB(final B b){
this.b = b;
return this;
}
public KeyBuilder withC(final C c){
this.c = c;
return this;
}
public Key build() {
return new Key(this.a, this.b, this.c);
}
}
I am trying to create an instance of Key using the builder with below spring configuration:
<!-- A -->
<bean id="A" class="com.example.A" />
<!-- B -->
<bean id="B" class="com.example.B" />
<!-- C -->
<bean id="C" class="com.example.C" />
<!-- KeyBuilder -->
<bean id="KeyBuilder" class="com.example.KeyBuilder">
<property name="a" value="A"/>
<property name="b" value="B"/>
<property name="c" value="C"/>
</bean>
<!-- Key -->
<bean id="Key" factory-bean="KeyBuilder" factory-method="build" />
However, it fails with below error:
Error creating bean with name 'KeyBuilder' defined in URL [file:spring-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'a' of bean class [com.example.KeyBuilder]: Bean property 'a' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
As I understand it would just work if I change the method names in builder from 'withA/withB/withC' to 'setA/setB/setC' but I do not have that option as the library is managed by a 3rd party.
Could someone please suggest if there is a way to pass setter method in the property tag or an alternate way to solve the issue here.