Spring中bean的生命周期

<strong><span style="font-size:24px;">1单例模式</span></strong>
<span style="font-size:18px;">public classMyFactory {  
   /** 
    * 这里使用单例设计模式 
    */  
   //这里使用单例模式设计,饿汉模式生成实例,没有使用就先生成  
   /* 
   privatestatic MyFactory instance=new MyFactory(); 
   publicstatic MyFactory getInstance(){ 
      returninstance; 
   } 
   */  
   /** 
    * 懒汉模式生成实例 
    */  
   private static MyFactory instance;  
   public static MyFactorygetInstance(){  
      /** 
       * 双重检查处理线程安全问题 
       */  
      synchronized (MyFactory.class) {  
         if(instance==null){  
            instance=new MyFactory();  
         }  
      }  
      return instance;  
   }  
   //私有构造,不允许外部创建  
   private MyFactory(){  
       
   }  
   public Object createObject(){  
      System.out.println("MyFactory.createObject()");  
      return new Object();  
   }  
}</span>

2.Bean生命周期

1.  User.java

<span style="font-size:18px;">public classUser {  
   private Long id;  
   private String name;  
   public User(){  
      System.out.println("创建User实例");  
   }  
   public Long getId() {  
      return id;  
   }  
   public void setId(Long id) {  
      this.id = id;  
   }  
   public String getName() {  
      return name;  
   }  
   public void setName(String name) {  
      this.name = name;  
   }  
}</span>
2.  applicationContext.xml

<span style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context"  
   xmlns:tx="http://www.springframework.org/schema/tx"  
   xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd  
            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
   <!-- scope属性:指定bean的生命周期  
      singleton:单例,ac.getBean()获取的是同一个实例  
      prototype:多例,ac,getBean()获取的是新实例  
      默认使用单例模式  
      lazy-init:指定在什么时候初始化单例对象  
      true:第一次调用getBean()获取本对象时才初始化  
      false:创建Application时候初始化单例对象  
      default:  
    -->  
   <bean id="user1" class="com.cloud.day7.User"scope="singleton" lazy-init="true">  
      <property name="name" value="Spring"/>  
   </bean>  
   <bean id="user2" class="com.cloud.day7.User"scope="prototype">  
      <property name="name" value="Summer"/>  
   </bean>  
</beans></span>
3. MainTest.java

import org.junit.Test;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public classMainTest {  
   private ApplicationContext ac=newClassPathXmlApplicationContext("applicationContext.xml",getClass());  
   /** 
    * 测试单例 
    * @throws Exception 
    */  
   @Test  
   public void test1() throws Exception {  
      System.out.println("---调用getBean()方法之前---");  
      UseruserA=(User) ac.getBean("user1");  
      UseruserB=(User) ac.getBean("user1");  
      System.out.println(userA!=null);  
      System.out.println(userA==userB);  
   }  
   /** 
    * 测试多例 
    * @throws Exception 
    */  
   @Test  
   public void test2() throws Exception {  
      System.out.println("---调用getBean()方法之前---");  
      UseruserA=(User) ac.getBean("user2");  
      UseruserB=(User) ac.getBean("user2");  
      System.out.println(userA!=null);  
      System.out.println(userA==userB);  
   }  
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 对于SpringBean生命周期Spring在容器有5个生命周期阶段,它们分别是:实例化、属性设置、初始化、销毁以及单例实例化,每个阶段的bean都会触发一个对应的回调事件,让开发者能够在每个阶段做一些额外的处理。 ### 回答2: SpringBean 生命周期包括以下几个阶段: 1. Bean 实例化:Spring 在启动时会通过反射机制实例化 Bean。这是 Bean 生命周期的开始阶段。 2. Bean 属性设置:Spring 在实例化 Bean 后,会根据配置文件或注解等方式,将属性值注入到 Bean 。 3. Bean 初始化前置处理:如果 Bean 实现了 InitializingBean 接口,那么 Spring 会在属性设置完成后调用 Bean 的 afterPropertiesSet() 方法进行初始化前的处理。 4. Bean 初始化后置处理:如果 Bean 配置了 init-method 方法,或者在配置文件通过 init-method 属性指定了初始化方法,那么 Spring 会在初始化前置处理完成后调用该方法。 5. Bean 使用阶段:在初始化完成后,Bean 就可以被应用程序使用了。 6. Bean 销毁前置处理:如果 Bean 实现了 DisposableBean 接口,那么在关闭应用程序或手动销毁 Bean 时,Spring 会先调用 Bean 的 destroy() 方法进行销毁前的处理。 7. Bean 销毁后置处理:如果 Bean 配置了 destroy-method 方法,或者在配置文件通过 destroy-method 属性指定了销毁方法,那么 Spring 会在销毁前置处理完成后调用该方法。 在整个 Bean 生命周期,开发人员可以通过实现 InitializingBean 和 DisposableBean 接口,或者在配置文件指定 init-method 和 destroy-method 方法,来自定义 Bean 的初始化和销毁过程,并在这些过程进行一些特定的操作。 ### 回答3: SpringBean生命周期可以分为以下阶段: 1. 实例化:Spring通过Bean定义创建Bean的实例。这可以通过构造函数实例化,或者通过工厂方法来实现。 2. 属性赋值:SpringBean的属性值注入到Bean的实例。这可以通过依赖注入(DI)方式进行,也可以通过配置文件或注解来实现。 3. 初始化:在Bean实例化和属性赋值之后,Spring会调用Bean的初始化方法。这可以通过实现InitializingBean接口的afterPropertiesSet()方法,或者使用@PostConstruct注解来实现。 4. 使用:在初始化完成之后,Bean可以被使用,执行业务逻辑。 5. 销毁:当Bean不再需要时,Spring会调用Bean的销毁方法。这可以通过实现DisposableBean接口的destroy()方法,或者使用@PreDestroy注解来实现。 需要注意的是,在Bean的生命周期,可以通过配置文件或注解来控制Bean的创建和销毁方式。 总的来说,SpringBean生命周期包括实例化、属性赋值、初始化、使用和销毁这几个阶段。通过控制Bean的生命周期,我们可以在合适的时机执行一些特定的操作,如初始化资源、释放资源等。这样可以有效地管理Bean的生命周期,提高系统的性能和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值