See
Spring example first.
If the AuthenticationService should use several dependent UserManagers, he can get a list or an array of UserManagers:
public class AuthenticationService {
private UserManager[] managers; public setUserManagers(UserManager[] managers) {
this.manager = manager;
}
public boolean authenticate(String name, String password) {
for (int i=0; i<managers.size(); i++) {
if (manager.exists(name)) {
User user = manager.get(name);
return user.getPassword().equals(password);
}
}
return false;
}
}You have to change the XML file
beans.xml to:
<beans>
<bean id="authenticationService" class="example.AuthenticationService">
<property name="userManagers">
<list>
<ref bean="simpleUserManager"/>
<ref bean="otherUserManager"/>
</list>
</property>
</bean>
<bean id="simpleUserManager" class="example.SimpleUserManager"/>
<bean id="otherUserManager" class="example.OtherUserManager"/>
</beans>
Java defined
Defining multiple dependencies of the same type in Java looks like this:
ListableBeanFactoryImpl beanFactory = new ListableBeanFactoryImpl();
MutablePropertyValues asPvs = new MutablePropertyValues();
List userManagerList = new ManagedList();
userManagerList.add(new RuntimeBeanReference("simpleUserManager"));
userManagerList.add(new RuntimeBeanReference("otherUserManager"));
asPvs.addPropertyValue("userManagers", userManagerList);
beanFactory.registerBeanDefinition("authenticationService",
new RootBeanDefinition(AuthenticationService.class, asPvs, true));
beanFactory.registerBeanDefinition("simpleUserManager",
new RootBeanDefinition(SimpleUserManager.class, null, true));
beanFactory.registerBeanDefinition("otherUserManager",
new RootBeanDefinition(SimpleUserManager.class, null, true));
// same as before
...