ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext();
ct.refresh();
ConfigurableListableBeanFactory bf = ct.getBeanFactory();
Ad bean = (Ad) bf.createBean(Ad.class);
System.out.println("bean ="+bean);
System.out.println("size= "+bf.getBeansOfType(Ad.class).size()); // print 0
广告类,这里是广告类信息,AD扩展了AbstractAd类:
public class Ad {
@Override
public String toString() {
return "ad[adid=" + this.getId() + "]";
}
}
这是日志:
[DEBUG] Creating instance of bean 'com.Ad'
[DEBUG] Finished creating instance of bean 'com.Ad'
bean = ad[adid=null]
size= 0
在我看来,大小应该是 1,有什么问题?
ps:最后我使用 GenericApplicationContext 和 BeanDefinition 并成功 createBean 并从上下文中获取,
GenericApplicationContext ct = new GenericApplicationContext();
ct.refresh();
ConfigurableListableBeanFactory bf = ct.getBeanFactory();
System.out.println("--------------start------------/n--------------------------/n-------------------/n");
BeanDefinition definition = new RootBeanDefinition(Ad.class);
ct.registerBeanDefinition("sampleService",
System.out.println(bf.getBeansOfType(Ad.class).size()); //print 1
日志:
[DEBUG] Creating instance of bean 'sampleService'
[DEBUG] Eagerly caching bean 'sampleService' to allow for resolving potential circular references
[DEBUG] Finished creating instance of bean 'sampleService'
1
但我仍然想知道:为什么 getBeansOfType(Ad.class).size()在 ClassPathXmlApplicationContext creteBean 之后为 0 ?