簡體   English   中英

Spring Framework 的 XML 配置和 Annotation 配置執行流程的區別

[英]Difference in execution flow of XML configuration vs Annotation configuration of Spring Framework

以下是在 Spring 框架中執行相同任務但具有不同配置的代碼。

一個.java

package com.kyc.spring;    
public class A {
    private int a;
    private String msg;
    static{
        System.out.println("A-S.B");
    }
    public A() {
        System.out.println("A-D.C");
    }
    public void setA(int a) {
        System.out.println("A-setA()");
        this.a = a;
    }
    public void setMsg(String msg) {
        System.out.println("A-setMsg()");
        this.msg = msg;
    }
    public String toString() {
        return ""+a+"\t"+msg;
    }
}

B.java

package com.kyc.spring;
public class B {
    private int b;
    private String msg;
    static{
        System.out.println("B-S.B");
    }
    public B() {
        System.out.println("B-D.C");
    }
    public B(int b, String msg) {
        System.out.println("B-2arg");
        this.b = b;
        this.msg = msg;
    }
    public String toString() {
        return ""+b+"\t"+msg;
    }
}

你好.java

package com.kyc.spring;
public class Hello {
    private A aobj;
    private B bobj;
    static{
        System.out.println("Hello-S.B");
    }
    public Hello() {
        System.out.println("Hello-D.C");
    }
    public void setAobj(A aobj) {
        System.out.println("Hello-setAobj()");
        this.aobj = aobj;
    }
    public void setBobj(B bobj) {
        System.out.println("Hello-setBobj()");
        this.bobj = bobj;
    }
    public Hello(A aobj, B bobj) {
        System.out.println("Hello-2arg");
        this.aobj = aobj;
        this.bobj = bobj;
    }
    public void show() {
        System.out.println(aobj);
        System.out.println(bobj);
    }
}

使用 XML 配置:

kyc.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">

    <bean id="aobj" class="com.kyc.spring.A" >
        <property name="a" value="10" />
        <property name="msg" value="I am A" />
    </bean>

    <bean id="bo" class="com.kyc.spring.B" >
        <constructor-arg value="20" />
        <constructor-arg value="I am B" />
    </bean>
    <bean id="hello" class="com.kyc.spring.Hello" autowire="byName" />
</beans>

測試1.java

package com.kyc.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("kyc.xml");
        System.out.println("Spring Container STARTS");
        System.out.println("--------------------------");
        Hello hello = (Hello)ctx.getBean("hello");
        hello.show();
    }
}

使用注釋配置:

Kyc.class

package com.kyc.spring;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Kyc {
    @Bean(autowire = Autowire.BY_NAME)
    public Hello hello(){
        return new Hello();
    }
    @Bean
    public A aobj(){
        A aobj = new A();
        aobj.setA(10);
        aobj.setMsg("I am A");
        return aobj;
    }
    @Bean
    public B bo(){
        return new B(20, "I am B");
    }
}

測試2.java

package com.kyc.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test2 {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(Kyc.class);
        System.out.println("Spring Container STARTS");
        System.out.println("--------------------------");
        Hello hello = (Hello) ctx.getBean("hello");
        hello.show();               
    }
}

現在讓我們看看 Test1 和 Test2 的輸出。

##for Test1:##
---------------
A-S.B
A-D.C
A-setA()
A-setMsg()
B-S.B
B-2arg
Hello-S.B
Hello-D.C
Hello-setAobj()
Spring Container STARTS.
--------------------------
10           I am A
null

##for Test2:##
--------------
Hello-S.B
Hello-D.C
A-S.B
A-D.C
A-setA()
A-setMsg()
B-S.B
B-2-arg
Spring Container STARTS.
--------------------------
10           I am A
null

為什么Test1.java 的輸出(或執行流程)與Test2.java 不相同?

要獲得可比較的結果,您應該在 kyc.xml 和 Kyc.java 中以相同的順序聲明您的 bean。

更改 kyc.xml 后,bean 的排序方式與 Kyc.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="hello" class="com.kyc.spring.Hello" autowire="byName" />

    <bean id="aobj" class="com.kyc.spring.A" >
        <property name="a" value="10" />
        <property name="msg" value="I am A" />
    </bean>

    <bean id="bo" class="com.kyc.spring.B" >
        <constructor-arg value="20" />
        <constructor-arg value="I am B" />
    </bean>
</beans>

test1 的輸出是

Hello-S.B
Hello-D.C
A-S.B
A-D.C
A-setA()
A-setMsg()
Hello-setAobj()
B-S.B
B-2arg
Spring Container STARTS
--------------------------
10  I am A
null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM