008---組件的 生命週期、InitializingBean, DisposableBean接口、JSR-250規範、BeanPostProcessor接口

spring中的bean生命週期:

構造 => 初始化前 => 初始化 => 初始化後 => 銷燬 

方式一:普通的java類,自定義初始化方法和銷燬方法 

@Bean 可以指定初始化和銷燬 回調方法,如下示例

普通的java類:

package bean;

public class HelloWorld {
    String hello="Hello demo";

    public HelloWorld() {
        super();
        System.out.println("導入成功");
    }
    //初始化方法
    public void init(){
        System.out.println("初始化。。。");
    }
    //銷燬方法
    public void destory(){
        System.out.println("銷燬。。。");
    }

    @Override
    public String toString() {
        return "HelloWorld{" +
                "hello='" + hello + '\'' +
                '}';
    }
}

@Bean註解配置的方式:  指定初始化 和 銷燬 的回調函數

package config;

import bean.HelloWorld;
import org.springframework.context.annotation.*;

@Configuration
public class Config {

    @Bean(initMethod = "init",destroyMethod = "destory")
    public HelloWorld helloWorld(){
        return new HelloWorld();
    }

}

 

 方式二:實現接口的方式(InitializingBean, DisposableBean)

使用spring提供的接口實現初始化和銷燬

package config;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class DemoInit implements InitializingBean, DisposableBean {
    //銷燬
    @Override
    public void destroy() throws Exception {
        System.out.println("DemoInit銷燬");
    }

    //初始化
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("DemoInit初始化");
    }
}

測試:配置類用@ComponentScan掃描將組件掃描進spring IOC容器中

package config;

import org.springframework.context.annotation.*;
@ComponentScan(value = "config")
@Configuration
public class Config {


}

測試類: 

package config;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


import static org.junit.Assert.*;

public class ConfigTest {

    @Test
    public void test1(){
        //初始化容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);

        //銷燬容器
        applicationContext.close();
    }
}

 

 

方式三:使用JSR-250規範註解(@PostConstruct、@PreDestroy)

首先導入jsr-250的jar包相關依賴

        <!-- https://mvnrepository.com/artifact/javax.annotation/jsr250-api -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

示例代碼

package config;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class DemoInit2 {

    public DemoInit2() {
    }
    @PostConstruct
    public void init(){
        System.out.println("初始化bean");
    }

    @PreDestroy
    public void destory(){
        System.out.println("銷燬bean");
    }
}

和方式二的結果一樣 

方式四:實現BeanPostProcessor接口(初始化的前後)

package config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class DemoInit3 implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println(o+"=>"+s);//打印bean初始化前的全類名和名稱
        return o;
    }

    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println(o+"=>"+s);//打印bean初始化前的全類名和名稱
        return o;
    }
}

 

 

 

發佈了259 篇原創文章 · 獲贊 15 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章