【Spring源码解析】组件注册-FactoryBean注册组件 2021-03-08 spring bean, spring, 注解, 源码 0 评论 实现 配置 12345678910111213141516public class PersonFactoryBean implements FactoryBean { @Override public Object getObject() throws Exception { return new Person("刘德华", 55); } @Override public Class<?> getObjectType() { return Person.class; } @Override public boolean isSingleton() { return true; }} 注册 12345678@Configurationpublic class BeanConfig2 { @Bean public PersonFactoryBean personFactoryBean() { return new PersonFactoryBean(); }} 测试 1234567891011121314/** * bean的类型:class cn.idea360.bean.Person * 工厂bean:class cn.idea360.bean.PersonFactoryBean */@Testpublic void test05() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig2.class); Object personFactoryBean = applicationContext.getBean("personFactoryBean"); System.out.println("bean的类型:" + personFactoryBean.getClass()); Object bean = applicationContext.getBean("&personFactoryBean"); System.out.println("工厂bean:" + bean.getClass());} 最后 本篇到此结束,欢迎大家关注公众号【当我遇上你】。