Bean的生命周期:
Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
Bean的定义
Bean 是 spring 装配的组件模型,一切实体类都可以配置成一个 Bean ,进而就可以在任何其他的 Bean 中使用,一个 Bean 也可以不是指定的实体类,这就是抽象 Bean 。
Bean的初始化
Spring中bean的初始化回调有两种方法
一种是在配置文件中声明init-method="init",然后在一个实体类中用init()方法来初始化
另一种是实现InitializingBean接口,覆盖afterPropertiesSet()方法。
第一种:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "init-one" class = "org.spring.test.BeanInitDemo1" init-method = "init" > < property name = "message" value = "这里是配置文件中为message赋值" ></ property > </ bean > </ beans > |
BeanInitDemo1类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package org.spring.test; public class BeanInitDemo1 { private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public void init(){ this .setMessage( "这里是init()方法初始化设值" ); } } |
测试类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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的赋值。
第二种:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "init-two" class = "org.spring.test.BeanInitDemo2" > < property name = "message" value = "这里是配置文件中为message赋值" ></ property > </ bean > </ beans > |
编写BeanInitDemo2类,使其实现InitializingBean接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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()方法设值" ); } } |
测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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:
1
2
|
BeanFactory factory= new XmlBeanFactory( new ClassPathResource( "bean.xml" )); factory.getBean( "student" ); |
BeanFactory是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用getBean方法才会抛出异常,也就是说当使用BeanFactory实例化对象时,配置的bean不会马上被实例化。当你使用该bean时才会被实例化(getBean)。
2, ApplicationContext:
1
|
ApplicationContext ac = new ClassPathXmlApplicationContext( "bean.xml" ); |
如果使用ApplicationContext,则配置的bean如果是singleton不管你用还是不用,都被实例化。ApplicationContext在初始化自身时检验,这样有利于检查所依赖属性是否注入。ApplicationContext是BeanFactory的子类,除了具有BeanFactory的所有功能外还提供了更完整的框架功能,例如国际化,资源访问等。所以通常情况下我们选择使用ApplicationContext。
Bean的销毁
Bean的销毁和初始化一样,都是提供了两个方法
一是在配置文件中声明destroy-method="cleanup",然后在类中写一个cleanup()方法销毁
二是实现DisposableBean接口,覆盖destory()方法
第一种:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "destory-one" class = "org.spring.test.BeanDestoryDemo1" destroy-method = "cleanup" > < property name = "message" value = "这里是配置文件中为message赋值" ></ property > </ bean > </ beans > |
BeanDestoryDemo1类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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( "销毁之前可以调用一些方法" ); } } |
测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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()方法。
第二种:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "destory-two" class = "org.spring.test.BeanDestoryDemo2" > < property name = "message" value = "这里是配置文件中为message赋值" ></ property > </ bean > </ beans > |
BeanDestoryDemo2类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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( "同样,销毁之前调用的方法" ); } } |
测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/weixin_36380516/article/details/72354668?utm_source=tuicool&utm_medium=referral