Spring第四章:IOP/DI

Posted reload

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring第四章:IOP/DI相关的知识,希望对你有一定的参考价值。

1.如何给 Bean 的属性赋值(注入)

  1.1通过构造方法设置值.

    1.1.1在 applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    

    <!--
        注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法
        需要手动的添加无参构造器 否则会出现错误  No default constructor found
       index  代表参数的位置  从0开始计算
       type   指的是参数的类型,在有多个构造函数时,可以用type来区分,要是能确定是那个构造函数,可以不用写type
       value  给基本类型赋值
       ref    给引用类型赋值
     -->
    <bean id="person_con" class="com.suncl.model.People">
        <constructor-arg index="0" type="java.lang.Long" value="1">
        </constructor-arg>
        <constructor-arg index="1" type="com.suncl.model.Student" ref="student_con"></constructor-arg>
    </bean>
    <bean id="student_con" class="com.suncl.model.Student">
        <constructor-arg index="0" type="java.lang.String" value="sName"></constructor-arg>
    </bean>

</beans>

 

 

 

    1.1.2实体类

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class Student {

    private String name;

    /**
     * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法
     需要手动的添加无参构造器 否则会出现错误  No default constructor found
     */
    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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


    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘‘‘ +
                ‘}‘;
    }


}

 

package com.suncl.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by SCL-PC on 2019/2/26.
 */
public class People {

    private Long pid;
    private String pname;
    private Student students;
    private List lists;
    private Set sets;
    private Map maps;
    private Properties properties;


    /**
     * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法
     需要手动的添加无参构造器 否则会出现错误  No default constructor found
     */
    public People(){

    }

    /**
     * 有参构造器
     * @param pid
     * @param students
     */
    public People(Long pid, Student students) {
        this.pid = pid;
        this.students = students;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Student getStudents() {
        return students;
    }

    public void setStudents(Student students) {
        this.students = students;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Set getSets() {
        return sets;
    }

    public void setSets(Set sets) {
        this.sets = sets;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }



    @Override
    public String toString() {
        return "People{" +
                "pid=" + pid +
                ", pname=‘" + pname + ‘‘‘ +
                ", students=" + students +
                ", lists=" + lists +
                ", sets=" + sets +
                ", maps=" + maps +
                ", properties=" + properties +
                ‘}‘;
    }
}

 

 

    1.1.3测试类

 

package com.suncl.test;

import com.suncl.model.People;
import com.suncl.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class TestDI {

    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");



        /**
         * 构造器注入
         */
        People peopleCon = actx.getBean("person_con",People.class);
        System.out.println(peopleCon.toString());
        Student studentCon = actx.getBean("student_con",Student.class);
        System.out.println(studentCon.toString());
    }

}

 

 

 

    1.1.4运行结果

 

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People{pid=1, pname=‘null‘, students=Student{name=‘sName‘}, lists=null, sets=null, maps=null, properties=null}
Student{name=‘sName‘}

 

 

 

  1.2设置注入(通过 set 方法)

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

    <bean id="people" class="com.suncl.model.People" >
        <!--set方式注入Long类型-->
        <property name="pid" value="1"></property>
        <!--set方式注入String类型-->
        <property name="pname" value="suncl"></property>
        <!--set方式注入对象-->
        <property name="students">
            <ref bean="student"/>
        </property>
        <!--set方式注入 List 三个元素-->
        <property name="lists">
            <list>
                <value>1</value>
                <ref bean="student"/>
                <value>vae1</value>
            </list>
        </property>
        <!--set方式注入 Set 三个元素 -->
        <property name="sets">
            <set>
                <value>1</value>
                <ref bean="student"/>
                <value>vae</value>
            </set>
        </property>

        <!--set方式注入 Map 2个元素 -->
        <property name="maps">
            <map>
                <entry key="m1" value="1"></entry>
                <entry key="m2" >
                    <ref bean="student" />
                </entry>
            </map>
        </property>

        <!--set方式注入 Properties 2个元素 -->
        <property name="properties">
            <props>
                <prop key="p1">p1</prop>
                <prop key="p2">p2</prop>
            </props>
        </property>
    </bean>

    <!--使用set方式注入-->
    <bean id="student" class="com.suncl.model.Student">
        <property name="name" value="sName"></property>
    </bean>


</beans>

 

 

 

 

    1.2.2实体类

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class Student {

    private String name;

    /**
     * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法
     需要手动的添加无参构造器 否则会出现错误  No default constructor found
     */
    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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


    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘‘‘ +
                ‘}‘;
    }


}

 

package com.suncl.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by SCL-PC on 2019/2/26.
 */
public class People {

    private Long pid;
    private String pname;
    private Student students;
    private List lists;
    private Set sets;
    private Map maps;
    private Properties properties;


    /**
     * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法
     需要手动的添加无参构造器 否则会出现错误  No default constructor found
     */
    public People(){

    }

    /**
     * 有参构造器
     * @param pid
     * @param students
     */
    public People(Long pid, Student students) {
        this.pid = pid;
        this.students = students;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Student getStudents() {
        return students;
    }

    public void setStudents(Student students) {
        this.students = students;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Set getSets() {
        return sets;
    }

    public void setSets(Set sets) {
        this.sets = sets;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }



    @Override
    public String toString() {
        return "People{" +
                "pid=" + pid +
                ", pname=‘" + pname + ‘‘‘ +
                ", students=" + students +
                ", lists=" + lists +
                ", sets=" + sets +
                ", maps=" + maps +
                ", properties=" + properties +
                ‘}‘;
    }
}

 

 

 

    1.2.3测试类

 

package com.suncl.test;

import com.suncl.model.People;
import com.suncl.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class TestDI {

    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");

        /**
         * set方式注入
         */
        People people = actx.getBean("people",People.class);
        System.out.println(people.toString());
        Student student = actx.getBean("student",Student.class);
        System.out.println(student.toString());


    }

}

 

 

 

 

    1.2.4运行结果

 

 

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People{pid=1, pname=‘suncl‘, students=Student{name=‘sName‘}, lists=[1, Student{name=‘sName‘}, vae1], sets=[1, Student{name=‘sName‘}, vae], maps={m1=1, m2=Student{name=‘sName‘}}, properties={p2=p2, p1=p1}}
Student{name=‘sName‘}

 

 

  1.3 DI

    1.3.1. DI:中文名称:依赖注入

    1.3.2. 英文名称((Dependency Injection)

    1.3.3. DI 是什么?

      1.3.3.1 DI IoC 是一样的

      1.3.3.2 当一个类(A)中需要依赖另一个类()对象时,B 赋值给 A 的过程就叫做依赖注入

      1.3.3.3 代码体现

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

    <!--依赖注入 DI-->
    <bean id="student" class="com.suncl.model.Student" >
        <property name="name" value="zhangsan"></property>
    </bean>

    <bean id="school" class="com.suncl.model.School">
        <property name="student" ref="student"></property>
    </bean>

</beans>

 

 

 

 

        1.3.3.3.2 实体类

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class School {

    private Student student;


    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "School{" +
                "student=" + student +
                ‘}‘;
    }
}

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class Student {

    private String name;

    /**
     * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法
     需要手动的添加无参构造器 否则会出现错误  No default constructor found
     */
    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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


    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘‘‘ +
                ‘}‘;
    }


}

 

 

 

        1.3.3.3.3测试类

 

package com.suncl.test;

import com.suncl.model.People;
import com.suncl.model.School;
import com.suncl.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class TestDI {

    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");


        /**
         * 依赖注入 创建学校的时候默认注入了一个学生
         */
        School school = actx.getBean("school",School.class);
        System.out.println(school.toString());

    }

}

 

 

 

 

        1.3.3.3.4运行结果

 

二月 27, 2019 11:45:34 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
School{student=Student{name=‘zhangsan‘}}

 

以上是关于Spring第四章:IOP/DI的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

报错spring实战(第四版)示例代码 ,使用 @DeclareParents 实现aop出错

Spring boot:thymeleaf 没有正确渲染片段

第四章:Spring AOP

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段