Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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 为什么@Autowired annotation将同一类的每个bean关联到context.xml中?_Java_Xml_Spring_Annotations_Autowired - Fatal编程技术网

Java 为什么@Autowired annotation将同一类的每个bean关联到context.xml中?

Java 为什么@Autowired annotation将同一类的每个bean关联到context.xml中?,java,xml,spring,annotations,autowired,Java,Xml,Spring,Annotations,Autowired,Context.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLo

Context.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"    xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />

<bean id="foo" class="annotation.Foo">
    <property name="name" value="firstFoo"></property>
</bean>
<bean id="secondaryFoo" class="annotation.Foo">
    <property name="name" value="secondaryFoo"></property>
    <qualifier value="secondaryFoo"></qualifier>
</bean>
<bean id="bar" class="annotation.Bar" />
Foo:

public class Bar {

@Autowired
@Qualifier(value="secondaryFoo")
private Foo foo;

public void setFoo(Foo foo) {
    this.foo = foo;
}
public void  printFooName(){
    System.out.println("\nBar class: foo.getName(): "+foo.getName()+"\n");      
}

}
public class Foo {
private String name;

public Foo(){
    System.out.println("\nFoo class: Constructor invoked, name: "+ name + "\n");
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

@PostConstruct
public void init() {
    System.out.println("\nFoo class: @PostConstruct Bean Foo successfully initialized, name: " + name +"\n");
}
@PreDestroy
public void cleanUp() {
    System.out.println("*\nFoo class: @PreDestroy clean up called\n");
}
}
public class TestFooBar {

public static void main(String[] args) throws InterruptedException {

    System.out.println("\nMain invoked\n");

    AbstractApplicationContext applicationContext = 
            new ClassPathXmlApplicationContext("annotation/annotation.xml");

    Bar bar = applicationContext.getBean("bar", Bar.class);

    bar.printFooName();

}

}
Main:

public class Bar {

@Autowired
@Qualifier(value="secondaryFoo")
private Foo foo;

public void setFoo(Foo foo) {
    this.foo = foo;
}
public void  printFooName(){
    System.out.println("\nBar class: foo.getName(): "+foo.getName()+"\n");      
}

}
public class Foo {
private String name;

public Foo(){
    System.out.println("\nFoo class: Constructor invoked, name: "+ name + "\n");
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

@PostConstruct
public void init() {
    System.out.println("\nFoo class: @PostConstruct Bean Foo successfully initialized, name: " + name +"\n");
}
@PreDestroy
public void cleanUp() {
    System.out.println("*\nFoo class: @PreDestroy clean up called\n");
}
}
public class TestFooBar {

public static void main(String[] args) throws InterruptedException {

    System.out.println("\nMain invoked\n");

    AbstractApplicationContext applicationContext = 
            new ClassPathXmlApplicationContext("annotation/annotation.xml");

    Bar bar = applicationContext.getBean("bar", Bar.class);

    bar.printFooName();

}

}
运行应用程序时,系统输出按以下顺序打印:

- Main invoked 
- Foo class: Constructor invoked, name: null 
- Foo class: @PostConstruct Bean Foo successfully initialized, name: firstFoo 
- Foo class: Constructor invoked, name: null 
- Foo class: @PostConstruct Bean Foo successfully initialized, name: secondaryFoo 
- Bar class: foo.getName(): secondaryFoo
如果我在Bar类(@Autowired@Qualifier(value=“secondary Foo”))中指定了具有secondary Foo id的bean,那么为什么在te上下文中,这两个bean都被实例化


如果我删除这两个注释,系统输出的结果是相同的!这怎么可能

annotation.xml
上下文中指定两个
Foo
bean,因此Spring会实例化这两个bean(以及
bar
bean)

然后,它将
secondaryFoo
作为依赖项注入到
bar
bean中


如果您不想自动创建bean,您可以像这样在
annotation.xml

中将它们声明为
lazy init=“true”
<代码>。。。使用lazy=“true”时,Spring给了我这个错误:org.springframework.beans.factory.xml.xmlbeandefinitionstoreexception,但我看到使用lazy属性时,bean的实例化是相同的。区别仅在于实例化的顺序。对吗?我只需要实例化firstFoo。这是可能的吗?如果你只想让firstFoo实例化,你希望注入到bar中的是什么?lazyinit将延迟初始化,直到需要它为止,即注入另一个bean或从上下文中查找。如果您将第一个和第二个foo都声明为lazy,那么由于您没有在任何地方使用firstFoo,所以应该实例化它。希望有帮助。