[ start | index | login ]
start > Spring example

Spring example

Created by funzel. Last edited by okiess, 8 years and 131 days ago. Viewed 21,955 times. #1
[edit] [rdf]
labels
attachments

Example on howto use Spring.

This text covers the changes from IoC example when to use Spring instead of PicoContainer.

Spring needs a setter for the UserManager (UM) so it can satisfy this dependency. The constructor like in IoC example is not needed.

public class AuthenticationService {
  private UserManager manager;

public setUserManager(UserManager manager) { this.manager = manager; }

public boolean authenticate(String name, String password) { if (manager.exists(name)) { User user = manager.get(name); return user.getPassword().equals(password); } else { return false; } } }

The recommended way to use Spring is to define dependencies with a an XML file beans.xml:

<beans>
  <bean id="authenticationService" class="example.AuthenticationService">
    <property name="userManager"><ref bean="simpleUserManager"/></property>
  </bean>
  <bean id="simpleUserManager" class="example.SimpleUserManager"/>
</beans>

To use our AuthenticationService, you do:

BeanFactory beanFactory = 
    new XmlBeanFactory(getClass().getResourceAsStream("/beans.xml"));

// just bean factory reference from now on // so the factory is also exchangeable AuthenticationService as = (AuthenticationService) beanFactory.getBean("authenticationService"); System.out.println("stephan.authenticated = " + as.authenticate("stephan", "stephan")); System.out.println("leo.authenticated = " + as.authenticate("leo", "wrong"));

The nice thing is that AS get's the UM that's available, SUM in this example. If we would change "SimpleUserManager" to "LDAPUserManager" then AS would get the LDAP manager for getting users. Easily exchangeable.

Java defined components

If you want to define component dependencies with Java instead of XML, this can be done in Spring with:

ListableBeanFactoryImpl beanFactory = new ListableBeanFactoryImpl();
MutablePropertyValues asPvs = new MutablePropertyValues();
asPvs.addPropertyValue("userManager", 
    new RuntimeBeanReference("simpleUserManager"));
beanFactory.registerBeanDefinition("authenticationService", 
    new RootBeanDefinition(AuthenticationService.class, asPvs, true));
beanFactory.registerBeanDefinition("simpleUserManager", 
   new RootBeanDefinition(SimpleUserManager.class, null, true));
  // just bean factory reference from now on, same as before
...

Resources

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