0
点赞
收藏
分享

微信扫一扫

008---组件的 生命周期、InitializingBean, DisposableBean接口、JSR-250规范、BeanPostProcessor接口

其生 2022-03-10 阅读 50


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;
}
}





举报

相关推荐

0 条评论