Spring——注入分类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37606901/article/details/82533103

Bean 实例在调用无参构造器创建了空值对象后,就要对 Bean 对象的属性进行初始化。
初始化是由容器自动完成的,称为注入。根据注入方式的不同,常用的有两类:设值注入、构造注入。还有另外一种,实现特定接口注入。由于这种方式采用侵入式编程,污染了代码,所以几乎不用。

(1)设值注入
设值注入是指,通过 setter 方法传入被调用者的实例。这种注入方式简单、直观,因而在 Spring 的依赖注入中大量使用。

先定义school的实体对象:

package com.bjpowernode.di01;

public class School {
    private String name;

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

    @Override
    public String toString() {
        return "School [name=" + name + "]";
    }
}

定义学生类对象实体:

package com.bjpowernode.di01;

public class Student {
    private String name;
    private int age;
    private School school;   // 对象属性,域属性

    public void setName(String name) {
        System.out.println("执行setName()");
        this.name = name;
    }
    public void setAge(int age) {
        System.out.println("执行setAge()");
        this.age = age;
    }

    public void setSchool(School school) {
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", school=" + school
                + "]";
    }
}

这里需要注意两个地方,一个是把school封装以后放在Student里面当作成员对象。第二个是在进行编辑实体对象的时候最好把所有的set/get方法添加上去(对于新手来说)。

配置文件:

<?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">

    <!-- 注册School -->
    <bean id="mySchool" class="com.bjpowernode.di01.School">
        <property name="name" value="清华大学"/>
    </bean>

    <!-- 注册Student ---下面的name要和Student里面的name进行一一对应--->
    <bean id="myStudent" class="com.bjpowernode.di01.Student">
        <property name="name" value="张三"/>
        <property name="age" value="23"/>
        <property name="school" ref="mySchool"/>
    </bean>

</beans>

当指定 bean 的某属性值为另一 bean 的实例时,通过 ref 指定它们间的引用关系。ref 的值必须为某bean 的 id 值。对于其它 Bean 对象的引用,除了标签的 ref 属性外,还可以使用标签。例如:




< ref bean=”mySchool”/>

测试类:

    package com.bjpowernode.di01;

    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;


    public class MyTest {

        @Test
        public void test01() {
            // 创建容器对象,加载Spring配置文件
            String resource = "com/bjpowernode/di01/applicationContext.xml";
            ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
            Student student = (Student) ac.getBean("myStudent");
            System.out.println(student);
        }

    }

(2)构造注入
构造注入是指,在构造调用者实例的同时,完成被调用者的实例化。即,使用构造器设置依赖关系。

构造注入标签中用于指定参数的属性有:
name:指定参数名称。
index:指明该参数对应着构造器的第几个参数,从 0 开始。不过,该属性不要也行, 但要注意,若参数类型相同,或之间有包含关系,则需要保证赋值顺序要与构造器中的参数顺序一致。另外,type 属性可用于指定其类型。基本类型直接写类型关键字即可,非基本类型需要写全限定性类名。

构造注入和设值注入在配置文件里面会有一些不同。

package com.bjpowernode.di02;

public class Student {
    private String name;
    private int age;
    private School school;   // 对象属性,域属性

    public Student(String name, int age, School school) {
        super();
        this.name = name;
        this.age = age;
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", school=" + school
                + "]";
    }
}

配置文件:

<?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">

    <!-- 注册School -->
    <bean id="mySchool" class="com.bjpowernode.di02.School">
        <property name="name" value="清华大学"/>
    </bean>

    <!-- 注册Student -->
    <bean id="myStudent" class="com.bjpowernode.di02.Student">

        <constructor-arg name="name" value="李四"/>
        <constructor-arg name="age" value="24"/>
        <constructor-arg name="school" ref="mySchool"/>
        <!-- 
        <constructor-arg index="0" value="李四"/>
        <constructor-arg index="1" value="24"/>
        <constructor-arg index="2" ref="mySchool"/>
         -->
        <!--  
        <constructor-arg value="李四"/>
        <constructor-arg value="24"/>
        <constructor-arg ref="mySchool"/>
         -->
    </bean>

</beans>

注意,上面的index可要可不要。


补充:(在设值注入的基础上进行的修改:)
对于设值注入与构造注入,在配置文件中,除了使用或标签外,还可使用命名空间注入的方式,让注入的值以标签属性的方式出现。根据注入实现方式的不同,分为p 命名空间注入与c 命名空间注入。
p 命名空间注入:采用设值注入方式,故需要有相应的 setter
c 命名空间注入:采用构造注入方式,故需要有相应的构造器

P命名空间注入和C命名空间注入是有点区别的:配置文件上面的区别

<?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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 注册School -->
    <bean id="mySchool" class="com.bjpowernode.di03.School" p:name="北京大学"/>

    <!-- 注册Student -->
    <bean id="myStudent" class="com.bjpowernode.di03.Student" 
            p:name="王五" p:age="25" p:school-ref="mySchool"/>

</beans>

C命名空间注入:

<?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:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 注册School -->
    <bean id="mySchool" class="com.bjpowernode.di04.School">
        <property name="name" value="清华大学"/>
    </bean>

    <!-- 注册Student -->
    <bean id="myStudent" class="com.bjpowernode.di04.Student" 
            c:name="赵六" c:age="26" c:school-ref="mySchool"/>

</beans>

猜你喜欢

转载自blog.csdn.net/qq_37606901/article/details/82533103