BeanFactoryAware

对于实现了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>
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值