Java 为什么@Autowired annotation将同一类的每个bean关联到context.xml中?
Context.xmlJava 为什么@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
<?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,所以应该实例化它。希望有帮助。