spring自动注入 单例 多例

670 单例 多例创建对象

问题:

Spring容器对象根据配置文件创建对象的时机默认发生在Spring容器对象在被创建的时候,也就是说,我们一旦获取到Spring容器对象,意味着可以直接获取Spring容器中的对象使用了.那么,如果我对同一个bean对象,连续获取N次,获取到的是不是同一个对象呢?因为spring容器对象底层使用的是map集合存储的bean对象,对map集合按照同一个键名获取数据,获取的是同一个,也就说按照同一个键名从Spring容器中获取的都是同一个对象,那么如果我们希望相同的键名获取的对象每次都不一样,怎么实现?

解决:

不要在Spring容器对象创建的时候,就完成对象的初始化创建,而是变为,从Spring容器中获取的时候才创建,每次获取都重新创建.

实现:

Spring容器的单例和多例模式创建对象.

单例模式(默认):

设置了单例模式的bean,会在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.bjsxt.pojo.Student"></bean>
</beans>
public class TestSpringIOC {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "applicationcontext.xml");
        Student student = (Student) ac.getBean("student");
        Student student1 = (Student) ac.getBean("student");
        System.out.println(student==student1);

    }
}
我是student无参数构造器
true	

设置了多例模式的bean,在Spring容器对象被创建的时候不会被初 始化创建,每次获取的时候都会重新创建,每次获取的对象都不相同.

<!--    多例模式-->
    <bean id="teacher" class="com.bjsxt.pojo.Teacher" scope="prototype"></bean>
//多例模式演示
Teacher teacher = (Teacher) ac.getBean("teacher");
Teacher teacher1 = (Teacher) ac.getBean("teacher");
System.out.println(teacher==teacher1);
我是teacher的五参数构造器
我是teacher的五参数构造器
false

671 自动注入介绍

问题:

在学习了SpringIOC的DI依赖注入后,我们可以根据对象之间的依赖关系的责任链,让Spring容器对象帮我们创建有一个组装好的对象,比如A中有B,B中有C,C中有D.将A,B ,C,D都创建为Bean对象,然后使用ref属性告诉Spring对象之间的依赖关系的组装规则,假如依赖责任链特别长,使用ref注入就会很麻烦.怎么办?

解决:

不要声明ref属性来指明依赖关系的注入,只需要告诉Spring容器对象依赖关系的注入规则,Spring容器对象自动根据规则完成依赖关系的注入.

实现:

①根据bean的ID和属性名一致的规则

②根据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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--手动依赖注入-->
    <bean id="stu" class="com.bjsxt.pojo.Student">
        <property name="sid" value="1"></property>
        <property name="sname" value="张三"></property>
        <property name="fav" value="唱歌"></property>
        <property name="teacher" ref="tea"></property>
    </bean>
    
    <bean id="tea" class="com.bjsxt.pojo.Teacher">
        <property name="tname" value="武老师"></property>
        <property name="tid" value="1"></property>
    </bean>
</beans>

⑤使用默认规则

672 自动注入 byName方式

public class Student {
    private Integer sid;
    private String sname;
    private String fav;
    private Teacher teacher;
<!--  byName  自动注入的使用-->
    <bean id="stu2" class="com.bjsxt.pojo.Student" autowire="byName">
        <property name="sid" value="1"></property>
        <property name="sname" value="张三"></property>
        <property name="fav" value="唱歌"></property>

    </bean>

    <bean id="teacher" class="com.bjsxt.pojo.Teacher">
        <property name="tname" value="武老师"></property>
        <property name="tid" value="1"></property>
    </bean>
public class TestSpringIOCAutoWired {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "applicationcontext.xml");


        //byName 自动注入的方式 id和属性名一致原则!!
        Student stu2 = (Student) ac.getBean("stu2");
        System.out.println(stu2);
    }
}
我是student无参数构造器
我是teacher的无参数构造器
Student{sid=1, sname='张三', fav='唱歌', teacher=Teacher{tid=1, tname='武老师'}}

673 自动注入 byType方式

public class Teacher {
    private Integer tid;
    private String tname;
public class Student {
    private Integer sid;
    private String sname;
    private String fav;
    private Teacher teacher;
<!--  byType  自动注入的使用-->
    <bean id="stu2" class="com.bjsxt.pojo.Student" autowire="byType">
        <property name="sid" value="1"></property>
        <property name="sname" value="张三"></property>
        <property name="fav" value="唱歌"></property>

    </bean>

    <bean id="tea2" class="com.bjsxt.pojo.Teacher">
        <property name="tname" value="武老师"></property>
        <property name="tid" value="1"></property>
    </bean>
public class TestSpringIOCAutoWired {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "applicationcontext.xml");


        //byName 自动注入的方式 bean的类型和属性的类型一致原则!!
        Student stu2 = (Student) ac.getBean("stu2");
        System.out.println(stu2);
    }
}
我是student无参数构造器
我是teacher的无参数构造器
Student{sid=1, sname='张三', fav='唱歌', teacher=Teacher{tid=1, tname='武老师'}}

674 自动注入 constructor方式

根据构造器形参的类型和某个bean的类型相同的规则,必须有对应的构造器!

!--  构造器  自动注入的使用-->
    <bean id="stu2" class="com.bjsxt.pojo.Student" autowire="constructor">
        <constructor-arg index="0" name="sid" type="java.lang.Integer" value="1"
        ></constructor-arg>
        <constructor-arg index="1" name="sname"
                         type="java.lang.String" value="张三"></constructor-arg>
        <constructor-arg index="2" name="fav" type="java.lang.String" value="唱歌"
        ></constructor-arg>

    </bean>

    <bean id="tea2" class="com.bjsxt.pojo.Teacher">
        <property name="tname" value="武老师"></property>
        <property name="tid" value="1"></property>
    </bean>
public class TestSpringIOCAutoWired {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "applicationcontext.xml");


        //byName 自动注入的方式 bean的类型一致和属性的类型一致原则!!
        Student stu2 = (Student) ac.getBean("stu2");
        System.out.println(stu2);
    }
}
我是teacher的无参数构造器
Student{sid=1, sname='张三', fav='唱歌', teacher=Teacher{tid=1, tname='武老师'}}

675 自动注入 全局配置

default-autowire=“byName”

<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"
        default-autowire="byName"
>

版权声明:本文为qq_32695257原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。