此接口定义了Bean的一些基本信息判断和获取bean比如
isSingleton isTypeMatch
还有一个需要注意的地方,看这个常量:
String FACTORY_BEAN_PREFIX = "&";
如果我们在获取Bean的时候,使用&则获取的是FactoryBean本身对象,否则是获取getObject的代理对象。
例子:
public class PersonFactoryBean implements FactoryBean<HelloService> {@Overridepublic HelloService getObject() throws Exception {return new HelloService();//可以做代理操作}@Overridepublic Class<?> getObjectType() {return HelloService.class;}@Overridepublic boolean isSingleton() {return true;}
}@Data
class HelloService {private String name;
}public class BeanDefinitionAndBeanDefinitionRegistryTest {public static void main(String[] args) {DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();beanFactory.registerBeanDefinition("helloService", new RootBeanDefinition(PersonFactoryBean.class));System.out.println(beanFactory.getBean("helloService"));System.out.println(beanFactory.getBean("&helloService"));}
}