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

 更新时间:2017年03月14日 09:06:48   投稿:daisy  
这篇文章主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

前言

在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中常见的负载均衡算法

    这篇文章主要介绍了关于Java中常见的负载均衡算法,负载平衡是一种电子计算机技术,用来在多个计算机、网络连接、CPU、磁盘驱动器或其他资源中分配负载,以达到优化资源使用、最大化吞吐率、最小化响应时间、同时避免过载的目的,需要的朋友可以参考下
    2023-08-08
  • JAVA面试题String产生了几个对象

    JAVA面试题String产生了几个对象

    这篇文章主要介绍了JAVA面试题 String s = new String("xyz");产生了几个对象?,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Redisson分布式锁的原理和代码实例

    Redisson分布式锁的原理和代码实例

    这篇文章主要介绍了Redisson分布式锁的原理和代码实例,在分布式系统中,锁机制是非常重要的,Redisson是一个基于Redis的Java应用程序,常常被应用作为分布式锁的解决方案,需要的朋友可以参考下
    2024-01-01
  • IntelliJ IDEA2025创建SpringBoot项目的实现步骤

    IntelliJ IDEA2025创建SpringBoot项目的实现步骤

    本文主要介绍了IntelliJ IDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-07-07
  • 带大家认识Java语法之泛型与通配符

    带大家认识Java语法之泛型与通配符

    使用泛型的目的是利用Java编译机制,在编译过程中帮我们检测代码中不规范的有可能导致程序错误的代码,下面这篇文章主要给大家介绍了关于Java泛型与通配符的相关资料,需要的朋友可以参考下
    2022-03-03
  • java生成申请单序列号的实现方法

    java生成申请单序列号的实现方法

    申请单序列号一般要求根据一定的规则生成后几位连续的字符串,下面是我项目中使用的生成序列号的代码,其中用到了锁机制,有需要的朋友可以参考一下
    2014-01-01
  • MyBatis中resultType和resultMap的用法及说明

    MyBatis中resultType和resultMap的用法及说明

    MyBatis通过resultType和resultMap将SQL查询结果映射为Java对象,resultType适用于简单类型或完全匹配的Java类,resultMap则用于处理列名和属性名不一致的情况
    2025-12-12
  • 通过实例解析java String不可变性

    通过实例解析java String不可变性

    这篇文章主要介绍了通过实例解析java String不可变性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 解决Nacos成功启动但是无法访问 (Connection refused)

    解决Nacos成功启动但是无法访问 (Connection refused)

    这篇文章主要介绍了解决Nacos成功启动但是无法访问 (Connection refused)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • Java中super和this的用法详解

    Java中super和this的用法详解

    这篇文章主要介绍了Java中super和this的用法详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08

最新评论