[ start | index | login ]
start > Advanced Spring example

Advanced Spring example

Created by funzel. Last edited by funzel, 8 years and 169 days ago. Viewed 10,428 times. #1
[edit] [rdf]
labels
attachments
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
  ...
no comments | post comment

What is Javangelist?
Javangelist is a website with help for professional Java developers.

1466 snips from 406 users.

Help
For text formatting help see snipsnap-help.

NEU: Jobs
Aktuelle Jobangebote.

Logged in Users: (0)
… and a Guest.



February 2012
SunMonTueWedThuFriSat
1234
567891011
12131415161718
19202122232425
26272829

Blogrolling:
>>Langreiter
>>Earl
>>henso
>>Lambda
>>e7l3

BlogShares
XHTML 1.0 validated
CSS validated
RSS 2.0 validated
RSS Feed

Powered by SnipSnap 1.0b3-uttoxeter

snipsnap.org | Copyright 2000-2005 Matthias L. Jugel and Stephan J. Schmidt