Spring----单例多例,懒加载及对象的创建

整个项目结构:

 

单例模式:对象在容器中全局唯一,在IOC容器初始化的时候该对象就会被创建。scope=“singleton”

多例模式:在每一次调用getBean的时候IOC容器才会创建该对象的实体,并且每次创建都是不同的对象。scope=“prototype”

 

懒加载:

用lazy-init。告诉spring容器是否以懒加载的方式创造对象。用的时候才加载构造,不用的时候不加载

取值:true(懒,真正调用到的时候再加载)、false(非懒,已启动spring容器就创建对象)、default(懒)

 

懒加载与非懒加载的优缺点:

懒加载:对象使用的时候才去创建,节省资源,但是不利于提前发现错误。

非懒加载:容器启动的时候立刻创建对象。消耗资源。利于提前发现错误。

当scope=“prototype” (多例)时,默认以懒加载的方式产生对象。

当scope=“singleton” (单例)时,默认以非懒加载的方式产生对象。

 

1.添加依赖

 <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    </dependencies>

2.在bean包创建一个Person类

package yx.swjd.bean;

public class Person {
    private int id;
    private String name;
    private String sex;

    public Person() {
        System.out.println("无参数构造方法");
    }

    public Person(int id, String name, String sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        System.out.println("带参数构造方法");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
    //初始化的方法
    public void init(){
        System.out.println("对象创建完就调用");
    }
    //销毁的方法
    public void destroy(){
        System.out.println("xhjdy");
    }
}

3.在resources中创建applicationCext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    id表示对象名,class表示对象从Person类创建-->
<!--    name是小名,有很多,id只一个-->
<!--    scope对象的创建规则 单例多例-->
<!--    lazy-init="true"对象的加载时机 懒加载 true 对象使用的时候才创建 false spring框架启动就加载-->
<!--    方式一  无参构造方法创建-->
    <bean id="p1" name="person1,person2" class="yx.swjd.bean.Person" scope="singleton" lazy-init="true" init-method="init" destroy-method="destroy">
<!--        di过程,依赖注入-->
        <property name="id" value="958"></property>
        <property name="name" value="yx"></property>
        <property name="sex" value="man"></property>
    </bean>

    <bean id="p2" name="person3,person4" class="yx.swjd.bean.Person" scope="prototype">
        <!--        di过程,依赖注入-->
        <property name="id" value="111"></property>
        <property name="name" value="zlx"></property>
        <property name="sex" value="woman"></property>
    </bean>

<!--    方式二  带参构造方法-->
    <bean id="p3" class="yx.swjd.bean.Person">
        <constructor-arg index="0" type="int" value="9528"></constructor-arg>
        <constructor-arg index="1" type="java.lang.String" value="yx"></constructor-arg>
        <constructor-arg index="2" type="java.lang.String" value="man"></constructor-arg>
    </bean>

    <!--    方式三  用静态工厂创建对象-->
    <bean id="p4" class="yx.swjd.beanfactory.PersonFactory" factory-method="createPerson">

    </bean>
    <!--    方式四  用非静态工厂创建对象-->
    <bean id="nostaticFactory" class="yx.swjd.beanfactory.NostaticFactory">

    </bean>
    <bean id="p5" factory-bean="nostaticFactory" factory-method="createPerson">

    </bean>
</beans>

 3.在beanfactory创建静态工厂和非静态工厂

package yx.swjd.beanfactory;

import yx.swjd.bean.Person;


public class NostaticFactory {
    public Person createPerson(){
        System.out.println("使用了非静态工厂");
        return new Person(1234,"xhh","man");
    }
}
package yx.swjd.beanfactory;

import yx.swjd.bean.Person;

//静态工厂
public class PersonFactory {
    //创建person的静态方法
    public static Person createPerson(){
        System.out.println("使用了静态工厂");
        return new Person(1234,"xhh","man");
    }
}

 4.最后创建一个test类对代码进行测试

package yx.swjd.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yx.swjd.bean.Person;

public class Mytest {
    public static void main(String[] args) {
        //        Person person=new Person();
//        System.out.println(person);

        //Spring容器的方式
//        启动并加载spring框架
        ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationCext.xml");
        //拿对象,为单例
        Person person1=context.getBean("person2",Person.class);
        person1.setName("zjl");
        Person person2=context.getBean("person2",Person.class);
        System.out.println(person1==person2);
        //手动关容器
        context.close();
    }
    @Test
    public void danli(){
        //        Person person=new Person();
//        System.out.println(person);

        //Spring容器的方式
//        启动并加载spring框架
        ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationCext.xml");
        //拿对象,为单例
        Person person1=context.getBean("person2",Person.class);
        person1.setName("zjl");
        Person person2=context.getBean("person2",Person.class);
        System.out.println(person1==person2);
    }
    @Test
    public void duoli(){
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("applicationCext.xml");
        Person person1=context.getBean("p2",Person.class);
        Person person2=context.getBean("p2",Person.class);
        System.out.println(person1==person2);
    }
    @Test
    public void daicangshu(){
        //带参数构造方法创建
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("applicationCext.xml");
        Person person=context.getBean("p3",Person.class);
        System.out.println(person);
    }
    @Test
    public void jingtaigongchang(){
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("applicationCext.xml");
        Person person=context.getBean("p4",Person.class);
        System.out.println(person);
    }
    @Test
    public void Nojingtaigongchang(){
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("applicationCext.xml");
        Person person=context.getBean("p5",Person.class);
        System.out.println(person);
    }
}

此篇博文记仅录自己的学习之路。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值