第四种组件注册方法:继承 FactoryBean接口,重写方法
1)默认获取到的是工厂bean调用getObject创建的对象
2)要获取工厂bean本身,我们需要给id前面加一个&,例如&colorFactoryBean
@Configuration public class MainConfig2 { @Bean public ColorFactoryBean colorFactoryBean(){ return new ColorFactoryBean(); } }
public class ColorFactoryBean implements FactoryBean { @Override public Object getObject() throws Exception { return new Color(); } @Override public Class<?> getObjectType() { return Color.class; } //是否单例 @Override public boolean isSingleton() { return false; } }