SpringBeanLifecycle
Lifecycle
- Spring创建Bean
- 执行set方法
- 执行前置方法
- 执行Bean Init 方法
- 执行后置方法
- 执行Bean Destroy 方法
BeanPostProcessor接口
一个Book类
package com.test.springdome01;
import java.util.*;
public class Book {
private int bId;
private String bName;
private Date createDate;
private String[] cite;
private List press;
private Map authors;
private Set catalogue;
public Book() {
System.out.println("this is book no arg constructor");
}
public Book(int bId, String bName) {
System.out.println("this is book arg constructor,bid:"+bId+",bName:"+bName);
this.bId = bId;
this.bName = bName;
}
public Book(int bId, String bName, Date createDate) {
System.out.println("this is book arg constructor");
this.bId = bId;
this.bName = bName;
this.createDate = createDate;
}
public void setbId(int bId) {
System.out.println("invoke book set method");
this.bId = bId;
}
public void setbName(String bName) {
this.bName = bName;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public void setCite(String[] cite) {
this.cite = cite;
}
public void setPress(List press) {
this.press = press;
}
public void setAuthors(Map authors) {
this.authors = authors;
}
public void setCatalogue(Set catalogue) {
this.catalogue = catalogue;
}
public int getbId() {
return bId;
}
public String getbName() {
return bName;
}
public Date getCreateDate() {
return createDate;
}
public String[] getCite() {
return cite;
}
public List getPress() {
return press;
}
public Map getAuthors() {
return authors;
}
public Set getCatalogue() {
return catalogue;
}
public void bookInit(){
System.out.println("invoke book init method");
}
public void bookDestroy(){
System.out.println("invoke book destroy method");
}
@Override
public String toString() {
return "Book{" +
"bId=" + bId +
", bName='" + bName + '\'' +
", createDate=" + createDate +
", cite=" + Arrays.toString(cite) +
", press=" + press +
", authors=" + authors +
", catalogue=" + catalogue +
'}';
}
}
前置处理类
class BookBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("invoke postProcessBeforeInitialization,id:"+beanName);
return null;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("invoke postProcessAfterInitialization,id:"+beanName);
return null;
}
}
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
">
<!-- 前置处理对象
factory-bean="book14" 绑定一个bean对象
-->
<bean id="BookBeanPostProcessor" class="com.test.springdome01.BookBeanPostProcessor" factory-bean="book14"></bean>
<!-- 普通bean对象 -->
<bean id="book14" class="com.test.springdome01.Book" init-method="bookInit" destroy-method="bookDestroy" >
<property name="bId" value="103"></property>
<property name="bName" value="spring"></property>
</bean>
<bean id="book15" class="com.test.springdome01.Book"></bean>
</beans>
//测试类
public class Test {
private ApplicationContext cpxac = new ClassPathXmlApplicationContext("application.xml");
@org.junit.Test
public void test14(){
System.out.println(cpxac.getBean("book14", Book.class));
System.out.println("---------------------------------------------------------");
System.out.println(cpxac.getBean("book15", Book.class));
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mRjIfQkw-1642596855291)(img\SpringFrameworkCore07SpringBeanLifecycle01.PNG)]
结论:与SpringLifecycle 一致