spring提供的几个常用可实现的接口

spring提供了很多可供用户实现的接口,当用户实现了这些接口。spring在不同的生命周期就会执行接口中相应的接口方法

比较重要的就是spring启动时的几个接口,这里只列了4个常用接口

BeanFactoryAware, BeanNameAware,
InitializingBean, DisposableBean

写个测试类

package demo;

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;

public class Person implements BeanFactoryAware, BeanNameAware,
        InitializingBean, DisposableBean {
    private String name;
         private String address;
         private String phone;
     
         private BeanFactory beanFactory;
         private String beanName;
     
         public Person() {
             System.out.println("【构造器】调用Person的构造器实例化");
         }
     
         public String getName() {
             return name;
         }
     
         public void setName(String name) {
             System.out.println("【注入属性】注入属性name");
             this.name = name;
         }
     
         public String getAddress() {
             return address;
         }
     
         public void setAddress(String address) {
             System.out.println("【注入属性】注入属性address");
             this.address = address;
         }
     
         public String getPhone() {
             return phone;
         }
     
         public void setPhone(String phone) {
             System.out.println("【注入属性】注入属性phone");
             this.phone = phone;
         }
     
             public String toString() {
             return "Person [address=" + address + ", name=" + name + ", phone="
                     + phone + "]";
         }
     
         // 这是BeanFactoryAware接口方法
         public void setBeanFactory(BeanFactory arg0) throws BeansException {
             System.out
                     .println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
             this.beanFactory = arg0;
         }
     
         // 这是BeanNameAware接口方法
         public void setBeanName(String arg0) {
             System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
             this.beanName = arg0;
         }
     
         // 这是InitializingBean接口方法
         public void afterPropertiesSet() throws Exception {
             System.out.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
         }
     
         // 这是DiposibleBean接口方法
         public void destroy() throws Exception {
             System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
         }
     
         // 通过<bean>的init-method属性指定的初始化方法
         public void myInit() {
             System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
         }
     
         // 通过<bean>的destroy-method属性指定的初始化方法
         public void myDestory() {
             System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
         }
}

启动后看到输入如下

【构造器】调用Person的构造器实例化
【注入属性】注入属性address
【注入属性】注入属性name
【注入属性】注入属性phone
【BeanNameAware接口】调用BeanNameAware.setBeanName()
【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()
【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
【init-method】调用<bean>的init-method属性指定的初始化方法

看到执行顺序为构造函数,属性注入

BeanNameAware.setBeanName()

BeanFactoryAware.setBeanFactory()

InitializingBean.afterPropertiesSet()

init-method

posted on 2018-11-09 09:13  liguangming  阅读(586)  评论(0编辑  收藏  举报

导航