✨生命周期
- 生命周期︰从创建到消亡的完整过程
- bean生命周期: bean从创建到销毁的整体过程
- bean生命周期控制:在bean创建后到销毁前做一些事情
✨配置的方式
🔥提供生命周期的控制方法
package com.study.dao.impl;
import com.study.dao.BookDao;
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save...");
}
private void destroy() {
System.out.println("destroy...");
}
private void init() {
System.out.println("init...");
}
}
🔥配置生命周期的控制方法
<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy"></bean>
✨接口控制
🔥实现InitializingBean, DisposableBean接口
package com.study.service.impl;
import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
import com.study.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
private BookDao bookDao;
public void save() {
System.out.println("BookServiceImpl...");
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void destroy() throws Exception {
System.out.println("Service Destroy...");
}
public void afterPropertiesSet() throws Exception {
System.out.println("Service afterPropertiesSet...");
}
}
✨案例:
🔥BookDao接口
package com.study.dao;
public interface BookDao {
void save();
}
🔥BookDaoImpl实现类
package com.study.dao.impl;
import com.study.dao.BookDao;
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save...");
}
private void destroy() {
System.out.println("destroy...");
}
private void init() {
System.out.println("init...");
}
}
🔥BookService接口
package com.study.service;
public interface BookService {
void save();
}
🔥BookServiceImpl实现类
package com.study.service.impl;
import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
import com.study.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
private BookDao bookDao;
public void save() {
System.out.println("BookServiceImpl...");
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void destroy() throws Exception {
System.out.println("Service Destroy...");
}
public void afterPropertiesSet() throws Exception {
System.out.println("Service afterPropertiesSet...");
}
}
🔥applicationContext.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="bookDao" class="com.study.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy"></bean>
<!--<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl"></bean>-->
<bean id="bookService" class="com.study.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
</beans>
🔥Test测试类
package com.study;
import com.study.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bean = context.getBean(BookDao.class);
bean.save();
context.close();
//context.registerShutdownHook();
}
}
/*
init...
book dao save...
destroy...
*/
/*
init...
Service afterPropertiesSet...
book dao save...
Service Destroy...
destroy...
*/