In all the examples of autowiring that I have found, the example is about one <bean> autowire attribute which is set for example to byName, and the has only one property value which is supposed to be set through autowiring.
My question is what if a <bean> has multiple properties that you want to set through autowiring? No one seems to explain that situation. Can someone explain if I can or if I should use autowire to set multiple properties in a bean? The following is an example of such a situation where I want to set the account and credit properties of the customer bean by autowiring:
<beans>
<bean name="customer" class="ultratech.com.Customer" autowire="byName">
<bean name="account"/>
<bean name="credit>
</beam>
<bean name="account" class="ultratech.com.Account"/>
<bean name="credit" class="ultratech.com.Credit"/>
</beans>
Also, please correct me if I’m wrong, but if I were to use annotation (@Autowire), then my problem is easily solved, since I would be able to add @Autowire to any property of a bean separately.
[EDIT: edited to reflect on updated question]
Your question is much more clear right now. You seem to think (if I follow your thinking properly), that in the
autowire="byName"you are supposed to provide a bean name instead ofbyNamevalue.That is not correct. The autowire attribute can take a few possible values and
byNameis one of those. When you setautowiretobyNamelike here:then Spring will look at all the fields in
someBean(foo.bar.Bazclass) and attempt to wire all fields of this object on a per name basis. That is, (in your case) if a Customer class has a fieldaccount, Spring will look in its context and try to find a bean with nameaccountto inject into the Customer bean.If you define two such beans:
then you are good to, if Customer is a class along this lines:
Here is what your XML code snippet should look like, assuming that your Customer class has fields named account and credit:
Apart from “byName” autowiring, you can autowire:
See Spring reference for more info:
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-autowire