XML配置的执行流程与Spring Framework的注释配置的区别

时间:2016-10-07 19:43:54

标签: java spring

以下是执行相同任务但在Spring框架中具有不同配置的代码。

A.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;
    }
}

Hello.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-Configuration:

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>

Test1.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();
    }
}

使用Annotation-Configuration:

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");
    }
}

Test2.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不同?

1 个答案:

答案 0 :(得分:2)

要获得可比较的结果,您应该在kyc.xml和Kyc.java中以相同的顺序声明bean。

更改kyc.xml以便以与Kyc.java中相同的方式对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">

    <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