1. 程式人生 > >SpringIOC容器初始化之後,和銷燬之前幾種方法的執行順序

SpringIOC容器初始化之後,和銷燬之前幾種方法的執行順序

1.搭建一個簡單的Spring環境

2.定義一個介面及其實現類

package com.roger.service;

public interface SpringLifeCycleService {

    void springLifeCycle();
}
package com.roger.service.impl;

import com.roger.service.SpringLifeCycleService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.atomic.AtomicInteger;

public class SpringLifeCycleServiceImpl implements SpringLifeCycleService, InitializingBean, DisposableBean {

    private AtomicInteger sortNum = new AtomicInteger(1);

    private SpringLifeCycleServiceImpl() {
        System.out.println(sortNum.getAndIncrement() + "-構造器方法開始執行....");
    }

    public void initMethod() {
        System.out.println(sortNum.getAndIncrement() + "-Spring XML配置檔案中新增init-method屬性的,在IOC容器初始化之後開始執行");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println(sortNum.getAndIncrement() + "-直接在介面實現類中定義一個方法,無需實現InitializingBean介面,添加註解@PostConstruct,在IOC容器初始化之後,執行的postConstruct()");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(sortNum.getAndIncrement() + "-實現InitializingBean介面,IOC容器初始化之後執行的afterPropertiesSet()");
    }

    public void destroyMethod() {
        System.out.println(sortNum.getAndIncrement() + "-Spring XML配置檔案中新增destroy-method屬性的,在IOC容器銷燬之前開始執行");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println(sortNum.getAndIncrement() + "-實現DisposableBean介面,IOC容器銷燬之前執行的destroy()");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println(sortNum.getAndIncrement() + "-直接在介面實現類中定義一個方法,無需實現DisposableBean介面,添加註解@PreDestroy,在IOC容器銷燬之前,執行的preDestroy()");
    }

    @Override
    public void springLifeCycle() {
        System.out.println(sortNum.getAndIncrement() +"-自定義介面的介面方法實現...");
    }
}

 3.定義spring的xml檔案

<?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.xsd">

    <bean id="springLifeCycleService" class="com.roger.service.impl.SpringLifeCycleServiceImpl"
        init-method="initMethod"
        destroy-method="destroyMethod"></bean>

</beans>

4.定義junit測試類

package com.roger.service.impl;

import com.roger.service.SpringLifeCycleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class SpringLifeCycleServiceImplTest {

    @Resource(name = "springLifeCycleService")
    private SpringLifeCycleService springLifeCycleService;

    @Test
    public void testSpringLifeCycle(){
        springLifeCycleService.springLifeCycle();
    }
}

5.測試結果

1-構造器方法開始執行....
2-直接在介面實現類中定義一個方法,無需實現InitializingBean介面,添加註解@PostConstruct,在IOC容器初始化之後,執行的postConstruct()
3-實現InitializingBean介面,IOC容器初始化之後執行的afterPropertiesSet()
4-Spring XML配置檔案中新增init-method屬性的,無需實現InitializingBean介面,在IOC容器初始化之後開始執行
5-自定義介面的介面方法實現...
6-直接在介面實現類中定義一個方法,無需實現DisposableBean介面,添加註解@PreDestroy,在IOC容器銷燬之前,執行的preDestroy()
7-實現DisposableBean介面,IOC容器銷燬之前執行的destroy()
8-Spring XML配置檔案中新增destroy-method屬性的,無需實現DisposableBean介面,在IOC容器銷燬之前開始執行