I have a class
class A{
private Foo foo;
private Bar bar;
private Baz baz;
}
Now this class has constructor that initializes foo and bar. Baz
however has an DataSource
field which I want o be injected with spring.
The A
class constructor initializes all but NOT baz
. Now the A
class in initialized with new
.
A a = new A(Foo, Bar)
The A
class has the setter.
Bean
<bean id="ABean" class="com.acme.A" >
<property name="baz">
<bean class="com.acme.baz">
<property name="dataSource" ref="mysqlDataSource"> </property>
</bean>
</property>
</bean>
but i keep getting:
nested exception is java.lang.NoSuchMethodException: com.acme.A.<init>()
1. How to inject only one property per bean while the rest is initialized with constructor?
2. What other way this can be solved?
For arguments to the constructor use the
constructor-arg
xml tag and for parameters that are set using setters use theproperty
xml tag.For that to work you need to define
setBaz()
method.Then you should use
getBean()
and not construct the object yourself. Let spring build it for you.