Wednesday, December 1, 2010

Using Enums in Spring XML config

Sometimes you need to initialize a bean using an enum. This can be achieved using the Util namespace in your XML config file as follows:

Some Java code:


Enum Weapon {
BATTLE_AXE("battle axe", 23),
LONG_BOW("longbow", 35),
BROAD_SWORD("broadsword", 25);

private String name;
private int damage;

private Weapon(String name, int damage){
this.name = name;
this.damage = damage;
}
}

public class Warrior{
private Weapon weapon;

public Warrior(Weapon weapon){
this.weapon = weapon;
}
...
}
Bean config:











No comments: