I have a bean in xml:
<bean id="fooBean" class="org.model.Foo" scope="prototype">
<property name="propOne" ref="fooBarOne"/>
<property name="propTwo" ref="fooBarTwo"/>
<property name="propThree" ref="fooBarThree"/>
</bean>
I converted it in java like this:
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public Foo fooBean(
FooBarOneImpl fooBarOne,
FooBarTwoImpl fooBarTwo,
FooBarThreeImpl fooBarThree
) {
Foo foo = new Foo();
foo.setPropOne(fooBarOne);
foo.setPropTwo(fooBarTwo);
foo.setPropThree(fooBarThree);
return foo;
}
When I execute:
(IFoo) applicationContext.getBean("fooBean", pizza, patatine, cocaCola);
with the xml config it works correctly, with Java config it gives I get an Exception.
The exception is the following:
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.model.Foo]: Illegal arguments to factory method 'fooBean'; args: "ciao","11","abc"; nested exception is java.lang.IllegalArgumentException: argument type mismatch
The class Foo has the constructor defined Foo(String pizza, String patatine, String cocaCola){...}.
Why when the bean is defined in the xml it finds the right constructor?
I tried to change the java conversion
Another critical topic: If you defined a CLASS return type, we can't be referencing the instance's interface.
If you use @Service/@Repository/@Component on your class, you can do ref because Spring will scan your project. Spring won't do it via @Bean because @Bean is the direct definition.