FactoryAware的引用
有些实例的创建可能是由其他类创建的例如 bean工厂创建bean的时候,就是通过BeanFactory接口完成的,如果在某些情况下,我们还需要创建者BeanFactory时。可以使用BeanFactoryAware.
举个例子
有个FactoryAware的接口
package com.chen.example;
public interface FactoryAware {
void setObeanFactory(ObeanFacory obeanFacory);
}
有个Obean实现了FactoryAware的接口
package com.chen.example;
public class Obean implements FactoryAware {
private ObeanFacory obeanFacory;
@Override
public void setObeanFactory(ObeanFacory obeanFacory) {
this.obeanFacory = obeanFacory;
}
}
当有其他类在创建Obean的时候可以吧自己带过去。
package com.chen.example;
public class ObeanFacory {
public Obean createObean(){
Obean obean = new Obean();
if(obean instanceof FactoryAware){
((FactoryAware) obean).setObeanFactory(this);
}
return obean;
}
}