1. 程式人生 > 其它 >spring——Spring自動裝配——示例

spring——Spring自動裝配——示例

1. 不使用自動裝配(autowire="no")

autowire="no" 表示不使用自動裝配,此時我們必須通過 <bean> 元素的 <constructor-arg>和 <property> 元素的 ref 屬性維護 Bean 的依賴關係。

2. 按名稱自動裝配(autowire="byName")

autowire="byName" 表示按屬性名稱自動裝配,XML 檔案中 Bean 的 id 或 name 必須與類中的屬性名稱相同。

3. 按型別自動裝配(autowire="byType")

autowire="byType" 表示按類中物件屬性資料型別進行自動裝配。即使 XML 檔案中 Bean 的 id 或 name 與類中的屬性名不同,只要 Bean 的 class 屬性值與類中的物件屬性的型別相同,就可以完成自動裝配。

4. 建構函式自動裝配(autowire="constructor")

autowire="constructor" 表示按照 Java 類中建構函式進行自動裝配。

5. 預設的自動裝配模式(autowire="default")

預設採用上一級標籤 <beans> 設定的自動裝配規則(default-autowire)進行裝配,Beans.xml 中的配置內容如下。 

=======================================================================

專案依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <
groupId>org.example</groupId> <artifactId>ssw</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.6</version> </dependency> </dependencies> </project>

dept類:

package org.example;

public class Dept {
    //部門編號
    private String deptNo;
    //部門名稱
    private String deptName;
    public Dept() {
        System.out.println("正在執行 Dept 的無參構造方法>>>>");
    }
    public Dept(String deptNo, String deptName) {
        System.out.println("正在執行 Dept 的有參構造方法>>>>");
        this.deptNo = deptNo;
        this.deptName = deptName;
    }
    public void setDeptNo(String deptNo) {
        System.out.println("正在執行 Dept 的 setDeptNo 方法>>>>");
        this.deptNo = deptNo;
    }
    public void setDeptName(String deptName) {
        System.out.println("正在執行 Dept 的 setDeptName 方法>>>>");
        this.deptName = deptName;
    }
    public String getDeptNo() {
        return deptNo;
    }
    public String getDeptName() {
        return deptName;
    }
    @Override
    public String toString() {
        return "Dept{" +
                "deptNo='" + deptNo + '\'' +
                ", deptName='" + deptName + '\'' +
                '}';
    }
}

employee類:

package org.example;

public class Employee {
    //員工編號
    private String empNo;
    //員工姓名
    private String empName;
    //部門資訊
    private Dept dept;
    public Employee() {
        System.out.println("正在執行 Employee 的無參構造方法>>>>");
    }
    public Employee(String empNo, String empName, Dept dept) {
        System.out.println("正在執行 Employee 的有參構造方法>>>>");
        this.empNo = empNo;
        this.empName = empName;
        this.dept = dept;
    }
    public void setEmpNo(String empNo) {
        System.out.println("正在執行 Employee 的 setEmpNo 方法>>>>");
        this.empNo = empNo;
    }
    public void setEmpName(String empName) {
        System.out.println("正在執行 Employee 的 setEmpName 方法>>>>");
        this.empName = empName;
    }
    public void setDept(Dept dept) {
        System.out.println("正在執行 Employee 的 setDept 方法>>>>");
        this.dept = dept;
    }
    public Dept getDept() {
        return dept;
    }
    public String getEmpNo() {
        return empNo;
    }
    public String getEmpName() {
        return empName;
    }
    @Override
    public String toString() {
        return "Employee{" +
                "empNo='" + empNo + '\'' +
                ", empName='" + empName + '\'' +
                ", dept=" + dept +
                '}';
    }
}

bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-autowire="constructor">

    <bean id="dept" class="org.example.Dept">
        <property name="deptNo" value="1"></property>
        <property name="deptName" value="技術部"></property>
    </bean>
    <bean id="employee" class="org.example.Employee" autowire="no">
        <property name="empNo" value="002"></property>
        <property name="empName" value="小郭"></property>
        <property name="dept" ref="dept"></property>
    </bean>

</beans>

執行:

package org.example;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main
{
    private static final Log LOGGER = LogFactory.getLog(Main.class);

    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Employee employee = context.getBean("employee", Employee.class);
        System.out.println(employee);

    }

}

執行結果:

========================================================

1. 不使用自動裝配(autowire="no")

autowire="no" 表示不使用自動裝配,此時我們必須通過 <bean> 元素的 <constructor-arg>和 <property> 元素的 ref 屬性維護 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-3.0.xsd" default-autowire="constructor">

    <bean id="dept" class="org.example.Dept">
        <property name="deptNo" value="1"></property>
        <property name="deptName" value="技術部"></property>
    </bean>
    <bean id="employee" class="org.example.Employee" autowire="no">
        <property name="empNo" value="002"></property>
        <property name="empName" value="小郭"></property>
        <property name="dept" ref="dept"></property>
    </bean>

</beans>

執行結果:

2. 按名稱自動裝配(autowire="byName")

autowire="byName" 表示按屬性名稱自動裝配,XML 檔案中 Bean 的 id 或 name 必須與類中的屬性名稱相同。

<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-3.0.xsd" >
    <bean id="dept" class="org.example.Dept">
        <property name="deptNo" value="1"></property>
        <property name="deptName" value="技術部"></property>
    </bean>
    <bean id="employee" class="org.example.Employee" autowire="byName">
        <property name="empNo" value="002"></property>
        <property name="empName" value="小郭"></property>
    </bean>
</beans>

3. 按型別自動裝配(autowire="byType")

autowire="byType" 表示按類中物件屬性資料型別進行自動裝配。即使 XML 檔案中 Bean 的 id 或 name 與類中的屬性名不同,只要 Bean 的 class 屬性值類中的物件屬性的型別相同,就可以完成自動裝配。

<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-3.0.xsd" >
    <bean id="dept2" class="org.example.Dept">
        <property name="deptNo" value="1"></property>
        <property name="deptName" value="技術部"></property>
    </bean>
    <bean id="employee" class="org.example.Employee" autowire="byType">
        <property name="empNo" value="002"></property>
        <property name="empName" value="小郭"></property>
    </bean>
</beans>

執行結果:

4. 建構函式自動裝配(autowire="constructor")

autowire="constructor" 表示按照 Java 類中建構函式進行自動裝配。

<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-3.0.xsd">
    <bean id="dept2" class="org.example.Dept">
        <constructor-arg name="deptNo" value="1"></constructor-arg>
        <constructor-arg name="deptName" value="技術部"></constructor-arg>
    </bean>
    <bean id="employee" class="org.example.Employee" autowire="constructor">
        <constructor-arg name="empNo" value="002"></constructor-arg>
        <constructor-arg name="empName" value="小郭"></constructor-arg>
    </bean>
</beans>

5. 預設的自動裝配模式(autowire="default")

預設採用上一級標籤 <beans> 設定的自動裝配規則(default-autowire)進行裝配,Beans.xml 中的配置內容如下。 

<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-3.0.xsd" default-autowire="byType">
    <bean id="dept2" class="org.example.Dept">
        <property name="deptNo" value="1"></property>
        <property name="deptName" value="技術部"></property>
    </bean>
    <bean id="employee" class="org.example.Employee" autowire="default">
        <property name="empNo" value="002"></property>
        <property name="empName" value="小郭"></property>
    </bean>
</beans>