public abstract class Product { public static final Product AAA = new Battery("AAA", 2.5); public static final Product CDRW = new Disc("CD-RW", 1.5); ... ... }
配置文件:
<beans ...> <bean id="aaa" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField"> <value>com.apress.springrecipes.shop.Product.AAA</value> </property> </bean> <bean id="cdrw" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField"> <value>com.apress.springrecipes.shop.Product.CDRW</value> </property> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <util:constant id="aaa" static-field="com.apress.springrecipes.shop.Product.AAA" /> <util:constant id="cdrw" static-field="com.apress.springrecipes.shop.Product.CDRW" /> </beans>