Spring中FactoryBean的使用方法
可以理解为工厂非静态方法的特例:
import org.joda.time.DateTime; import org.springframework.beans.factory.FactoryBean; public class NextDayDateFactoryBean implements FactoryBean { @Override public Object getObject() throws Exception { // TODO Auto-generated method stub return new DateTime().plus(1); } @Override public Class getObjectType() { // TODO Auto-generated method stub return DateTime.class; } @Override public boolean isSingleton() { // TODO Auto-generated method stub return false; } }
<bean id="nextDayDate" class="com.uqee.spring.beans.NextDayDateFactoryBean"></bean>
此时调用getBean("nextDayDate")返回的是NextDayDateFactoryBean实例返回的DateTime对象,而不是
NextDayDateFactoryBean对象, 要得到该FactoryBean可通过如下方法访问:
Object o2 = factory.getBean("&nextDayDate");