对于实现了org.springframework.beans.factory.BeanFactoryAware接口的类, 当它被BeanFactory创建后,它会拥有一个指向创建它的BeanFactory的引用。 也就是说光实现BeanFactoryAware接口还是不够得,那个实现BeanFactoryAware接口的类必须是由spring管理的,不然在你那个类里面创建BeanFactory是不会成功的.(spring容器在解析配置文件的时候,会实例化那些由他管理的bean,当然其中肯定有一个就是我们的那个实现了beanFactoryWare接口的类,在实例化的过程中,会把beanfactory 注入到setBeanFactory(BeanFactory beanFactory)方法里面去)
public interface BeanFactoryAware {
/**
* Callback that supplies the owning factory to a bean instance.
* <p>Invoked after the population of normal bean properties
* but before an initialization callback such as
* {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
* @param beanFactory owning BeanFactory (never <code>null</code>).
* The bean can immediately call methods on the factory.
* @throws BeansException in case of initialization errors
* @see BeanInitializationException
*/
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
具体代码: package ch5.BeanAware;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import ch5.destroy.ShutDownHook;
public class SimpleBean implements InitializingBean,DisposableBean,BeanNameAware,BeanFactoryAware,Runnable ...{
private ConfigurableListableBeanFactory configurableListableBeanFactory;
public void setBeanName(String beanName) ...{
System.out.println("this is info from BeanNameAware and beanName is:"+beanName);
}
public void setBeanFactory(BeanFactory factory) throws BeansException ...{
//自动注册成线程
if(factory instanceof ConfigurableListableBeanFactory)...{
System.out.println("this is info from BeanNameAware and beanName");
this.configurableListableBeanFactory=(ConfigurableListableBeanFactory)factory;
Runtime.getRuntime().addShutdownHook(new Thread(this));
}
}
public void afterPropertiesSet() throws Exception ...{
System.out.println("this is info from afterpropertiesSet from SimpleBean");
}
public void run() ...{
//线程调用destroySingletons
configurableListableBeanFactory.destroySingletons();
}
private String name;
private String sex;
private String age;
public void destroyMethod()...{
System.out.println("this is info from customer destroy method");
}
public void destroy()...{
System.out.println("this is info from destroy method");
}
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
package ch5.BeanAware;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring...{
public static void main(String args[]) throws Exception...{
//获取bean factory
ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory(); //使用子beanFactory
factory.preInstantiateSingletons();
SimpleBean bean1=(SimpleBean)factory.getBean("SimpleBean");
}
public static BeanFactory getBeanFactory()...{
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/destroy"+File.separator+"applicationContext.xml";
ConfigurableListableBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="SimpleBean" class="ch5.destroy.SimpleBean" destroy-method="destroyMethod">
<property name="name">
<value>gaoxiang</value>
</property>
<property name="sex">
<value>male</value>
</property>
<property name="age">
<value>26</value>
</property>
</bean>
</beans>