Spring第八章:Spring自動注入

.自動注入

  1.Spring 配置文件中對象名和 ref=idid 名相同使用自動注入,能夠不配置<property/>

  2.兩種配置辦法

    2.1 <bean>中經過 autowire=」」 配置,只對這個<bean>生效

    2.2 <beans>中經過 default-autowire=」」配置,表噹噹前文件中所<bean>都是全局配置內容

    2.3 自動注入代碼

      2.3.1測試自動注入的實體類,老師和學生類

 

package com.suncl.model;

import org.springframework.beans.factory.annotation.Value;

/**
 * Created by SCL-PC on 2019/3/5.
 */

public class Teacher {
    private String name ="張三";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                '}';
    }
}

 

 
 
package com.suncl.model;

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;

/**
* Created by SCL-PC on 2019/3/5.
*/
public class Student {
private String name = "李四";
private Teacher teacher22;

public Student(Teacher teacher22){
this.teacher22 = teacher22;
}

public Student() {
}

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

public Teacher getTeacher22() {
return teacher22;
}

public void setTeacher22(Teacher teacher22) {
this.teacher22 = teacher22;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", teacher=" + teacher22 +
'}';
}
}
 

      2.3.2配置文件

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

<!--默認的注入方式 配置以後取beans的 default-autowire 若是默認的也沒有配置就等同no-->
<bean id="student1" class="com.suncl.model.Student" autowire="default"></bean>

<!--不自動注入-->
<bean id="student2" class="com.suncl.model.Student" autowire="no"></bean>

<!--按照名稱自動注入 也就是id-->
<bean id="student3" class="com.suncl.model.Student" autowire="byName"></bean>

<!--按照類型自動注入 注意這裏若是有兩個相同的注入類型會報錯-->
<bean id="student4" class="com.suncl.model.Student" autowire="byType"></bean>

<!--按照構造器自動注入-->
<bean id="student5" class="com.suncl.model.Student" autowire="constructor"></bean>


<bean id="teacher" class="com.suncl.model.Teacher"></bean>

<!--寫來測試按照類型注入的時候出錯的場景-->
<!--<bean id="teacher2" class="com.suncl.model.Teacher"></bean>-->

</beans>

      2.3.3 測試類

package com.suncl.test;

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

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class Test {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)ac.getBean("student3",Student.class);
        System.out.println(student.toString());

    }


}

 

 

 

    3.autowire=」」 可取值

 

      3.1 default: 默認值,根據全局 default-autowire=」」值.默認全局和局部都沒有配置狀況下,至關於 no

 

  <!--默認的注入方式 配置以後取beans的 default-autowire 若是默認的也沒有配置就等同no-->
<bean id="student1" class="com.suncl.model.Student" autowire="default"></bean>

Student student = (Student)ac.getBean("student1",Student.class);
Student{name='李四', teacher=null}

      運行結果能夠看到 老師實體沒有完成注入 可是咱們若是配置了一個全局 default-autowire=」」再看下結果java

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

    <!--默認的注入方式 配置以後取beans的 default-autowire 若是默認的也沒有配置就等同no-->
    <bean id="student1" class="com.suncl.model.Student" autowire="default"></bean>
Student{name='李四', teacher=Teacher{name='張三'}} 注入成功 

 

 

      3.2 no: 不自動注入

 <!--不自動注入-->
 <bean id="student2" class="com.suncl.model.Student" autowire="no"></bean>
Student student = (Student)ac.getBean("student2",Student.class);
Student{name='李四', teacher=null}

      不注入就只能是null了spring

 

 

      3.3 byName: 經過名稱自動注入.Spring 容器中找類的 Id 

<bean id="student3" class="com.suncl.model.Student" autowire="byName"></bean>
Student student = (Student)ac.getBean("student3",Student.class);
Student{name='李四', teacher=Teacher{name='張三'}}

 

       能夠看到按照名稱注入了,怎麼理解名稱注入呢。假設咱們如今修改下bean的idapp

<bean id="teacher22" class="com.suncl.model.Teacher"></bean>
 Student student = (Student)ac.getBean("student3",Student.class);
Student{name='李四', teacher=null}

      而後再執行測試類,發現沒法注入成功。ide

      而後咱們再去修改學生類的屬性,修改teacher屬性爲teacher22完成注入測試

   private Teacher teacher22;
  public Teacher getTeacher22() {
        return teacher22;
    }

    public void setTeacher22(Teacher teacher22) {
        this.teacher22 = teacher22;
    }
Student student = (Student)ac.getBean("student3",Student.class);
Student{name='李四', teacher=Teacher{name='張三'}}

      以下便可注入成功,因此byName的注入方式要求bean的id 和注入類的屬性名稱一致便可this

      3.4 byType: 根據類型注入

 

 <bean id="student4" class="com.suncl.model.Student" autowire="byType"></bean>
Student student = (Student)ac.getBean("student4",Student.class);
Student{name='李四', teacher=Teacher{name='張三'}}

 

      特別注意容器裏面不能夠出現兩個相同類型的beanspa

    <!--寫來測試按照類型注入的時候出錯的場景-->
    <bean id="teacher2" class="com.suncl.model.Teacher"></bean>

      例如我這邊就定義了兩個相同類型的Teacher實體。那麼spring會出現錯誤code

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.suncl.model.Teacher] is defined: expected single matching bean but found 2: teacher22,teacher2
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1288)
    ... 18 more
expected single matching bean but found 2: teacher22,teacher2。預期是一個知足的bean,可是找到了2個,立馬就懵逼了。

 

 

      3.5 constructor: 根據構造方法注入.

        3.5.1 提供對應參數的構造方法(構造方法參數中包含注入對戲那個)

 

        3.5.2 底層使用 byName, 構造方法參數名和其餘<bean>id

 

 public Student(Teacher teacher22){
        this.teacher22 = teacher22;
 }

 

        提供一個屬性名稱的構造器xml

  <!--按照構造器自動注入-->
<bean id="student5" class="com.suncl.model.Student" autowire="constructor"></bean>
Student student = (Student)ac.getBean("student5",Student.class);
Student{name='李四', teacher=Teacher{name='張三'}}

        注意:這裏定義了屬性名稱構造器以後,spring會提示有上面的byName和byType會出現錯誤,須要再手動添加一個無參構造器。java默認會爲全部的類提供無參構造器,對象

        可是若是你本身寫了任何有參數的構造器,那麼默認的無參構造器就沒有了。

 

        連接:https://pan.baidu.com/s/154ae2zW9qN8KhMpmODD65g

相關文章
相關標籤/搜索