首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring 進階實戰:Java 代碼與 XML 配置等價轉換深度解析

     編輯:Java 綜合教學

Spring 進階實戰:Java 代碼與 XML 配置等價轉換深度解析

實際開發中極少將代碼轉為 XML 配置,但理解二者的對應關係能幫助開發者深入掌握 Spring 容器的 Bean 管理機制。本文通過兩個完整案例,演示如何用 XML 配置實現 Java 代碼的對象創建、依賴注入與方法調用。

案例一:基本對象創建與屬性注入

Java 代碼

// WangYang.java
public class WangYang {
    private int age;
    // getter/setter/toString 省略
}

// Person.java
public class Person {
    private int age;
    private WangYang wy;
    // getter/setter 省略
}

// MainTest.java
public class MainTest {
    public static void main(String[] args) {
        WangYang wy = new WangYang();
        wy.setAge(10);
        Person person = new Person();
        person.setAge(20);
        person.setWy(wy);
        System.out.println(person.getWy());
    }
}

等價 XML 配置

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans 
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- WangYang wy = new WangYang(); wy.setAge(10); -->
    <bean id="wy" class="com.siti.spring20160311.WangYang">
        <property name="age" value="10"/>
    </bean>

    <!-- Person person = new Person(); person.setAge(20); person.setWy(wy); -->
    <bean id="person" class="com.siti.spring20160311.Person">
        <property name="age" value="20"/>
        <property name="wy" ref="wy"/>
    </bean>

    <!-- 獲取 person.wy 屬性值(相當於 person.getWy()) -->
    <bean name="wyTest" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
        <property name="propertyPath" value="wy"/>
        <property name="targetBeanName" value="person"/>
    </bean>
</beans>

測試類

public class MainTest4SpringConf {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext20160311.xml");
        System.out.println(context.getBean("wyTest"));
    }
}

案例二:Swing GUI 組件的 XML 配置構建

構建一個包含 JTextArea、JPanel、JButton 的 JFrame 視窗。

關鍵技術點:

  • MethodInvokingFactoryBean:調用實例方法(如 frame.add()、panel.add()),將返回值或副作用納入容器管理
  • util:constant:引用常量(如 BorderLayout.SOUTH)
  • 構造器注入:<constructor-arg>

XML 配置節選

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:util="https://www.springframework.org/schema/util"
       xsi:schemaLocation="https://www.springframework.org/schema/beans 
       https://www.springframework.org/schema/beans/spring-beans.xsd
       https://www.springframework.org/schema/util 
       https://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <bean name="frame" class="javax.swing.JFrame">
        <constructor-arg value="窗體"/>
        <property name="visible" value="true"/>
    </bean>

    <bean name="textArea" class="javax.swing.JTextArea">
        <constructor-arg value="10"/>
        <constructor-arg value="20"/>
    </bean>

    <!-- frame.add(new JScrollPane(textArea)) -->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="frame"/>
        <property name="targetMethod" value="add"/>
        <property name="arguments">
            <list>
                <bean class="javax.swing.JScrollPane">
                    <constructor-arg ref="textArea"/>
                </bean>
            </list>
        </property>
    </bean>

    <!-- 其餘組件配置類似,包含 panel、buttonYes、buttonNo、pack() 調用 -->
</beans>

核心收穫

Spring 容器本質上是一個強大的「對象工廠」,XML 配置與 Java 代碼完全等價。掌握 PropertyPathFactoryBean、MethodInvokingFactoryBean 等工廠 Bean,能處理複雜的屬性獲取與方法調用場景,深化對 IoC 容器底層機制的理解。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved