Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XML配置与Spring框架注释配置的执行流程差异_Java_Spring - Fatal编程技术网

Java XML配置与Spring框架注释配置的执行流程差异

Java XML配置与Spring框架注释配置的执行流程差异,java,spring,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 v

下面是在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;
    }
}
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;
    }
}
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);
    }
}
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();
    }
}
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();               
    }
}
B.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;
    }
}
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;
    }
}
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);
    }
}
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();
    }
}
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();               
    }
}
Hello.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;
    }
}
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;
    }
}
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);
    }
}
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();
    }
}
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();               
    }
}
使用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>
使用注释配置: 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;    
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;
    }
}
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;
    }
}
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);
    }
}
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();
    }
}
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的输出。。
为什么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>