Spring Framework

springframework.org

Dave Brondsema

Overview

excellent docs spring has become a defacto standard for IoC

Before Spring

Interface and Implementation

public interface Weapon { public void fire(float dir, float x, float y); } public class SimpleGun implements Weapon { ... public void fire(float dir, float x, float y) { Projectile newProj = projectile.createProjectile(); newProj.setDirection(dir); newProj.setX(x); newProj.setY(y); log.info("set dir,x,y, = " + dir + " " + x + " " + y); getManager().addProjectile(newProj); } ... }

Implementation Choice

public class Ship { // simple protected Weapon weapon = new SimpleGun(); } public class Ship { // consolidate all weapon creation into a "factory" protected Weapon weapon = WeaponFactory.getFactory().createWeapon(); }

Dependency Injection / Inversion of Control

JavaBean conventions

public class Ship extends PhysicalObject implements MouseInputListener { protected Weapon weapon; public void setWeapon(Weapon weapon) { this.weapon = weapon; } public Weapon getWeapon() { return weapon; } }

Spring XML Config file

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="simpleGun" class="net.sf.deimos.weapons.SimpleGun"> <property name="projectile" ref="missile"/> </bean> <bean id="ship" class="net.sf.deimos.physics.Ship"> <property name="weapon" ref="simpleGun"/> <property name="collisionBehavior" ref="fatalCollision"/> </bean>
various shortcuts, other things (e.g., creating many projectiles) can be programmatically constructed instead of XML 2.0 had domain-specific notation

Kicking off the Spring container

ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Universe universe = (Universe) appContext. getBean("universe"); universe.begin();
often used in web apps; register a listener in web.xml plus web-tier integration libraries

Integration

sometimes the integration is overkill; although it is consistent across ORM frameworks, for example

Integration, cont.

  • Web MVC (Model-View-Controller)
  • Other web frameworks, portlets
  • Web Services
  • EJBs
  • JMS, JMX, email, scheduling
  • web framework integration is often to get bean access into the framework