Spring 常用接口之 InitializingBean,Di

2019-04-11  本文已影响0人  炒面Z

InitializingBean 接口,只有一个方法 afterPropertiesSet

afterPropertiesSet()方法将在spring所有的属性被初始化完毕调用

DisposableBean 接口,只有一个方法 destroy

destroy()方法在spring容器销毁前执行

代码测试

@Service
public class Test implements InitializingBean, DisposableBean {

  @Override
  public void destroy() throws Exception {
    System.out.println("执行destroy()");
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    System.out.println("执行afterPropertiesSet()");
  }
}

项目启动-控制台

四月 11, 2019 9:00:06 下午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
执行afterPropertiesSet()
[2019-04-11 09:00:10,473] Artifact AsyncMailSend:war exploded: Artifact is deployed successfully
[2019-04-11 09:00:10,473] Artifact AsyncMailSend:war exploded: Deploy took 6,853 milliseconds

项目关闭-控制台

D:\dev\apache-tomcat-7.0.81\bin\catalina.bat stop
Disconnected from the target VM, address: '127.0.0.1:10042', transport: 'socket'
四月 11, 2019 9:00:19 下午 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
四月 11, 2019 9:00:19 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-apr-8089"]
四月 11, 2019 9:00:19 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-apr-8222"]
四月 11, 2019 9:00:19 下午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
执行destroy()
四月 11, 2019 9:00:19 下午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["http-apr-8089"]
四月 11, 2019 9:00:19 下午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["ajp-apr-8222"]
...
  • Spring容器中的Bean几种初始化前方法和销毁后方法的先后顺序
  • 1.注解PostConstruct,PreDestroy
  • 2.实现接口InitializingBean,DisposableBean
  • 3.在xml中配置init-method,destroy-method (指定初始化后和销毁前执行的方法,必须无参)
/**
 * @author ssm
 * @version V1.0
 * @description: Spring容器中的Bean几种初始化前方法和销毁后方法的先后顺序
 * <p>1.注解PostConstruct,PreDestroy<p/>
 * <p>2.实现接口InitializingBean,DisposableBean<p/>
 * <p>3.在xml中配置init-method,destroy-method (指定初始化后和销毁前执行的方法,必须无参)<p/>
 *
 * @date 2019/4/11 20:58
 */
@Service
public class Test implements InitializingBean, DisposableBean {

    public Test() {
        System.out.println("construct方法");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("@PostConstruct注解的方法将会在依赖注入完成后被自动调用");
    }

    @PreDestroy
    public void PreDestroy() {
        System.out.println("PreDestroy()");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("执行destroy()");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行afterPropertiesSet() 方法是在Bean的属性设置之后才会进行调用");
    }
}

执行结果(先后顺序):

  • 启动 contruct -> PostConstruct -> afterPropertiesSet
  • 关闭 PostDetroy -> destroy
上一篇下一篇

猜你喜欢

热点阅读