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:
Bean config:
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;
}
...
}


No comments:
Post a Comment