Spring中bean的生命周期

来源:互联网 发布:java入门代码例子 编辑:程序博客网 时间:2024/06/16 19:00
<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
原创粉丝点击