spring之Bean的生命周期详解

网友投稿 196 2023-05-14


spring之Bean的生命周期详解

Bean的生命周期:

Bean的定义——Bean的初始化——Bean的使用——Bean的销毁

Bean的定义

Bean 是 spring 装配的组件模型,一切实体类都可以配置成一个 Bean ,进而就可以在任何其他的 Bean 中使用,一个 Bean 也可以不是指定的实体类,这就是抽象 Bean 。

Bean的初始化

Spring中bean的初始化回调有两种方法

一种是在配置文件中声明init-method="init",然后在一个实体类中用init()方法来初始化

另一种是实现InitializingBean接口,覆盖afterPropertiesSet()方法。

第一种:

配置文件:

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

BeanInitDemo1类:

package org.spring.test;

public class BeanInitDemo1 {

private StringRPvqgUoE message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public void init(){

this.setMessage("这里是init()方法初始化设值");

}

}

测试类:

package org.spring.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

BeanInitDemo1 bid = (BeanInitDemo1) context.getBean("init-one");

System.out.println(bid.getMessage());

}

}

运行结果:

这里是init()方法初始化设值

原因:init()初始化方法的调用是在配置文件的Bean初始化之后执行的, 所以改变了配置文件中对message的赋值。

第二种:

配置文件:

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

编写BeanInitDemo2类,使其实现InitializingBean接口

package org.spring.test;

import org.springframework.beans.factory.InitializingBean;

public class BeanInitDemo2 implements InitializingBean{

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public void afterPropertiesSet() throws Exception {

// TODO Auto-generated method stub

this.setMessage("这里覆盖了InitializingBean接口的afterPropertiesSet()方法设值");

}

}

测试:

package org.spring.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

BeanInitDemo2 bid = (BeanInitDemo2) context.getBean("init-two");

System.out.println(bid.getMessage());

}

}

运行结果: 这里覆盖了InitializingBean接口的afterPropertiesSet()方法设值

原因相同,afterPropertiesSet()方法在配置文件的Bean初始化后执行,所以改变了配置文件中对message的赋值

Bean的使用

Spring中有两种使用bean的方法:

1, BeanFactory:

BeanFactory factory= new XmlBeanFactory(new ClassPathResource("bean.xml"));

factory.getBean("student");

BeanFactory是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用getBean方法才会抛出异常,也就是说当使用BeanFactory实例化对象时,配置的bean不会马上被实例化。当你使用该bean时才会被实例化(getBean)。

2, ApplicationContext:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

如果使用ApplicationContext,则配置的bean如果是singleton不管你用还是不用,都被实例化。ApplicationContext在初始化自身时检验,这样有利于检查所依赖属性是否注入。ApplicationContext是BeanFactory的子类,除了具有BeanFactory的所有功能外还提供了更完整的框架功能,例如国际化,资源访问等。所以通常情况下我们选择使用ApplicationContext。

Bean的销毁

Bean的销毁和初始化一样,都是提供了两个方法

一是在配置文件中声明destroy-method="cleanup",然后在类中写一个cleanup()方法销毁

二是实现DisposableBean接口,覆盖destory()方法

第一种:

配置文件:

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

BeanDestoryDemo1类:

package org.spring.test;

public class BeanDestoryDemo1 {

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public void cleanup(){

System.out.println("销毁之前可以调用一些方法");

}

}

测试:

package org.spring.test;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DestortTest {

public static void main(String[] args) {

AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

BeanDestoryDemo1 bdd = (BeanDestoryDemo1) context.getBean("destory-one");

System.out.println(bdd.getMessage());

context.registerShutdownHook();

}

}

运行结果:

context.registerShutdownHook()是为spring注册关闭吊钩,程序退出之前关闭spring容器,如果没有

context.registerShutdownHook();将不会执行cleanup()方法。

第二种:

配置文件:

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

BeanDestoryDemo2类:

package org.spring.test;

import org.springframework.beans.factory.DisposableBean;

public class BeanDestoryDemo2 implements DisposableBean{

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public void destroy() throws Exception {

// TODO Auto-generated method stub

System.out.println("同样,销毁之前调用的方法");

}

}

测试:

package org.spring.test;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DestortTest {

public static void main(String[] args) {

AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

BeanDestoryDemo2 bdd = (BeanDestoryDemo2) context.getBean("destory-two");

System.out.println(bdd.getMessage());

context.registerShutdownHook();

}

}

运行结果:

Spring可以管理singleton作用域的Bean的生命周期,所以在Bean初始化及销毁之前可以做一些工作,更灵活的管理Bean。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue2.0中goods选购栏滚动算法的实现代码
下一篇:Vue声明式渲染详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~