java - 为什么@Autowired注释将同一类的每个bean关联到context.xml中?

标签 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: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" />

栏:

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

}

Foo:

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")) 中指定具有辅助 Foo id 的 bean,为什么在上下文中这两个 bean 都会被实例化?

如果我去掉这两个注解,系统输出的结果是一样的!怎么可能呢?

最佳答案

您在 annotation.xml 上下文中指定了两个 Foo bean,因此 Spring 实例化了它们(以及您的 bar bean) .

然后,此外,它还会将 secondaryFoo 作为依赖项注入(inject)到您的 bar bean 中。

如果您不希望自动创建 bean,您可以在 annotation.xml 中将它们声明为 lazy-init="true"

关于java - 为什么@Autowired注释将同一类的每个bean关联到context.xml中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31952787/

相关文章:

java - JPA EntityManager 在 @Transactional 方法之后阻塞

java - Imcache Spring 配置

java - 从 headless Eclipse 导出 war

java 将 int 转换为短整型

java - 对象类型可以转换为组件类型吗?

java - 如何在 Android Studio 中引用界面组件

xml - XSLT:属性内的 'xsl:value-of'

Android XML 解析异常

java - 如何利用 Spring MVC 实现前端 Controller 但不使用 Controller

java - 如何找到数组中最接近的元素