Spring入門-----------------屬性注入和對象注入


屬性注入即通過setter方法注入bean的屬性或依賴對象。

屬性注入使用<property>元素,使用name屬性指定bean的屬性的名稱,value屬性或<value>子節點指定屬性值。

原理:通過java的反射機制,調用此屬性的setter方法。所以改屬性必須有setter方法才能使用。

構造器注入:

通過構造方法注入bean的屬性值或依賴的對象,它保證了bean實例在實例化后就可以使用。

構造器注入在<constructor-arg>初始化屬性值,name屬性表示通過參數名稱設置屬性。

構造器注入有兩種方法:

  1. 按索引匹配入參

通過參數的順序進行初始化

  1. 按類型匹配入參

通過方法的參數類型定位是用哪個構造方法

 

1.創建一個Student類

package com.xt.spring.student;

public class Student {

    private String stuNo;
    private String stuName;
    public School school;
    public School getSchool() {
        return school;
    }
    public void setSchool(School school) {
        this.school = school;
    }
    public  Student(String stuNo,String stuName){
        this.stuNo=stuNo;
        this.stuName=stuName;
    }
    public Student(){
        
    }
    public String getStuNo() {
        return stuNo;
    }
    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    @Override
    public String toString() {
        return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", school=" + school + "]";
    }
}

 新建一個school類,作為引用對象

package com.xt.spring.student;

public class School {
 private String address;
 private String detail;
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getDetail() {
    return detail;
}
public void setDetail(String detail) {
    this.detail = detail;
}
@Override
public String toString() {
    return "School [address=" + address + ", detail=" + detail + "]";
}
 
}

 

2.創建配置文件applicationContext-stu.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
               http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util.xsd ">

<!--
 DI:依賴注入 javabean通過標簽property向實體中的屬性注入值。屬性必須有setter方法,不然會有異常。 原理:通過java的反射機制,調用此屬性的setter方法 通過構造方法實例化bean對象,並初始化屬性值: 通過標簽constructor-arg 初始化屬性值,name屬性表示通過參數名稱設置屬性
  <bean id="stu" class="com.xt.spring.student.Student">
  <constructor-arg type="java.lang.String" value="20162430730"></constructor-arg>
  <constructor-arg type="java.lang.String" value="楊賀龍"></constructor-arg>
  </bean>
     痛過設置參數的書序進行初始化,屬性index表示參數順序

          <bean id="stu" class="spring.ioc.test.stu.Student">
          <constructor-arg value="20162430730" index="0"></constructor-arg>
          <constructor-arg value="楊賀龍" index="1"></constructor-arg>
          </bean>


      通過方法的參數類型定位使用哪個構造方法
      <constructor-arg type="java.lang.String" value="楊賀龍"></constructor-arg>
 向javabean屬性注入一個引用:
        通過bean標簽的子標簽property標簽向javabean屬性注入一個引用,
        屬性name表示bean的屬性名稱,ref表示注入的引用bean Name或者bean ID;
    
        在使用<property>標簽向bean中注入值時,如果注入的值包含特殊符號需要使用<![CDATA[值]]以 value標簽的形式注入。
   引用對象時使用ref屬性,對應取值為引用對象的id
 -->

<bean id="school" class="com.xt.spring.student.School">
<property name="address" value="鄭州大學"></property>
<property name="detail" value="信息工程學院"></property>
</bean>

<bean id="stu" class="com.xt.spring.student.Student">

<property name="stuNo" value="20162430730"></property>

<property name="stuName" value="楊賀龍"></property>

<property name="school" ref="school"></property>

</bean>

</beans>

 

3.pom.xml文件與上個博客中的相同

4.實現類

package com.xt.spring.student;

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

public class IOCTest {
    private ApplicationContext ioc;
@Before
public void Init(){
    ioc = new ClassPathXmlApplicationContext("Spring/ApplicationContext-stu.xml");
}
@Test
public void Test(){
    Student stu = ioc.getBean("stu",Student.class);
    System.out.println("==========="+stu);    
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM