详解Spring中Bean的生命周期和作用域及实现方式

所属分类: 软件编程 / java 阅读数: 30
收藏 0 赞 0 分享

前言

在applicationContext.xml中配置完bean之后,Bean的声明周期状态有哪些。生命周期的各个阶段可以做什么。在applicationContext.xml配置bean的作用域有哪些。其中各个作用域代表的是什么。适用于什么情况。这篇文章做一个记录。

生命周期

初始化

可以直接查看图片,图片来自Spring Bean Life Cycle

从上图看出,Bean初始化完成包括9个步骤。其中一些步骤包括接口的实现,其中包括BeanNameAware接口,BeanFactoryAware接口。ApplicationContextAware接口。BeanPostProcessor接口,InitializingBean接口。那么这些接口在整个生命周期阶段都起到什么作用?后面我们一一介绍。

实例化前

当Bean全部属性设置完毕后,往往需要执行一些特定的行为,Spring提供了两种方式来实现此功能:

  • 使用init-mothod方法
  • 实现initializingBean接口

指定初始化方法

如下:

package com.model;
public class InitBean {
 public static final String NAME = "mark";
 public static final int AGE = 20;
 
 public InitBean() {
 // TODO Auto-generated constructor stub
 System.out.println("执行构造方法");
 }
 
 public String name;
 public int age ;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 
 public void init(){
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
}

编写加载器

package com.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserServiceImpl;
public class Main {
 public static void main(String[] args) { 
 ApplicationContext context = new ClassPathXmlApplicationContext("initbean.xml");
 InitBean bean = (InitBean) context.getBean("init");
 }
}

配置Bean

注意init-method参数

<?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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="init" class="com.model.InitBean" init-method="init"/>
</beans>

执行结果

实现InitializingBean接口

实现InitializingBean接口会实现afterPropertiesSet方法,这个方法会自动调用。但是这个方式是侵入性的。一般情况下,不建议使用。

实现afterPropertiesSet方法

package com.model;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean {
 public static final String NAME = "mark";
 public static final int AGE = 20;
 
 public InitBean() {
 // TODO Auto-generated constructor stub
 System.out.println("执行构造方法");
 }
 
 public String name;
 public int age ;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 
 public void init(){
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
}

配置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"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> -->
 <bean id="init" class="com.model.InitBean" init-method="init"/>
</beans>

结果:

销毁

同样,上图中表示来Bean销毁时候的过程。包括DisposableBean接口。

使用destroy-method方法

package com.model;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean {
 public static final String NAME = "mark";
 public static final int AGE = 20;
 
 public InitBean() {
 // TODO Auto-generated constructor stub
 System.out.println("执行构造方法");
 }
 
 public String name;
 public int age ;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 
 public void init(){
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 
 public void close(){
 System.out.println("bean被销毁");
 }
}

配置Bean

<?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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> -->
 <bean id="init" class="com.model.InitBean" destroy-method="close"/>
</beans>

配置加载器

package com.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserServiceImpl;
public class Main {
 public static void main(String[] args) { 
 AbstractApplicationContext context = new ClassPathXmlApplicationContext("initbean.xml");
 context.registerShutdownHook();
 InitBean bean = (InitBean) context.getBean("init");
 }
}

结果:

实现DisposableBean接口

实现DisposableBean接口

package com.model;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean,DisposableBean {
 public static final String NAME = "mark";
 public static final int AGE = 20;
 
 public InitBean() {
 // TODO Auto-generated constructor stub
 System.out.println("执行构造方法");
 }
 
 public String name;
 public int age ;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 
 public void init(){
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("调用init方法进行成员变量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 
 public void close(){
 System.out.println("bean被销毁");
 }
 @Override
 public void destroy() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("bean被销毁完成");
 }
}

配置文件

<?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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> -->
 <!-- <bean id="init" class="com.model.InitBean" destroy-method="close"/> -->
 <bean id="init" class="com.model.InitBean"/>
</beans>

Spring Bean的作用域

作用域 描述
singleton 该作用域将 bean 的定义的限制在每一个 Spring IoC 容器中的一个单一实例(默认)。
prototype 该作用域将单一 bean 的定义限制在任意数量的对象实例。
request  该作用域将 bean 的定义限制为 HTTP 请求。只在 web-aware Spring ApplicationContext 的上下文中有效。
session 该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。
global-session 该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。

配置示例

<bean id="..." class="..." scope="singleton">
</bean>

使用方法注入协调作用域不同的Bean

正常情况下,如果singleton作用域依赖singleton作用域。即每次获取到的都是一样的对象。同理,prototype作用域依赖prototype作用域,每次获取到的都是新的对象。但是,如果singleton依赖prototype作用域,那么每次获取到的singleton中的prototype都是第一次创建的prototype。如何协调这种关系。保证每次获取到的都是正确的呢。

对于这种情况,Spring提供了lookup方法用来解决这种问题。

首先我们定义一个原型:

package com.model;
public class MyHelper {
 public void doSomethingHelpful() {
 
 }
}

然后通过接口注入:

package com.model;
public interface DemoBean {
 MyHelper getHelper();
 void somePeration();
}

配置一个单例:

package com.model;
/**
 * 测试类
 * @author kevin
 *
 */
public abstract class AbstractLookupDemo implements DemoBean {
 
 public abstract MyHelper getMyHelper();
 
 @Override
 public MyHelper getHelper() {
 // TODO Auto-generated method stub
 return getMyHelper();
 }
 
 @Override
 public void somePeration() {
 // TODO Auto-generated method stub
 
 }
}

配置文件:

<?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"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="helper" class="com.model.MyHelper" scope="prototype"/>
  <bean id="standardLookupBean" class="com.model.StandardLookupDemo">
  <property name="myHelper" ref="helper"></property>
  </bean>
  <bean id = "abstractLookupBean" class="com.model.AbstractLookupDemo">
  <lookup-method name="getMyHelper" bean="helper"></lookup-method>
  </bean>
</beans>

加载配置文件:

package com.model;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class Main {
 public static void main(String[] args) {
 
 AbstractApplicationContext context = new ClassPathXmlApplicationContext("lookBean.xml");
 context.registerShutdownHook();
 System.out.println("传递standardLookupBean");
 test(context, "standardLookupBean");
 System.out.println("传递AbstractLookupDemo");
 test(context, "abstractLookupBean");
 }
 
 public static void test(AbstractApplicationContext context,String beanName) {
 DemoBean bean = (DemoBean) context.getBean(beanName);
 MyHelper helper1 = bean.getHelper();
 MyHelper helper2 = bean.getHelper();
 System.out.println("测试"+beanName);
 System.out.println("两个helper是否相同?"+(helper1==helper2));
 StopWatch stopWatch = new StopWatch();
 stopWatch.start("lookupDemo");
 for (int i = 0; i < 10000; i++) {
 MyHelper helper = bean.getHelper();
 helper.doSomethingHelpful();
 }
 stopWatch.stop();
 System.out.println("获取10000次花费了"+stopWatch.getTotalTimeMillis()+"毫秒");
 }
}

结果:

从上面的结果图看出,以前的方式生成的对象每次都是相同的。通过lookup方式注入每次是不同的。可以解决这种问题。但是有没有更简单的方式,感觉这种方式优点麻烦。

让Bean感知Spring容器

实现BeanNameAware,自定设置id值。

实现BeanFactoryAware,ApplicationContextAware 感知Spring容器。获取Spring容器。

Spring国际化支持

配置配置文件

<?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"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
   <list>
   <value>message</value>
   </list>
  </property>
  </bean>
</beans>

新建中文配置文件

message_zh_CN.properties:

hello=welcome,{0}
now=now is : {0}

新建英文配置文件

message_en_US.properties:

hello=\u4F60\u597D,{0}
now=\u73B0\u5728\u7684\u65F6\u95F4\u662F : {0}

加载配置文件

package com.model;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class Main {
 public static void main(String[] args) {
 ApplicationContext context = new ClassPathXmlApplicationContext("globalization.xml");
 String[] a = {"读者"};
 String hello = context.getMessage("hello",a, Locale.CHINA);
 Object[] b = {new Date()};
 String now = context.getMessage("now",b, Locale.CHINA);
 System.out.println(hello);
 System.out.println(now);
 hello = context.getMessage("hello",a, Locale.US);
 now = context.getMessage("now",b, Locale.US);
 System.out.println(hello);
 System.out.println(now);
 }
}

结果

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Java的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多