BeanNameAware and BeanFactoryAware Interfaces in Spring – Spring中的BeanNameAware和BeanFactoryAware接口

最后修改: 2018年 5月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’re going to focus on the BeanNameAware and BeanFactoryAware interfaces, in the Spring Framework.

在这个快速教程中,我们将重点介绍Spring框架中的BeanNameAwareBeanFactoryAware接口

We’ll describe each interface separately with the pros and cons of their usage.

我们将分别描述每个界面,并说明其使用的优点和缺点。

2. Aware Interface

2、Aware界面

Both BeanNameAware and BeanFactoryAware belong to the org.springframework.beans.factory.Aware root marker interface. This uses setter injection to get an object during the application context startup.

BeanNameAwareBeanFactoryAware都属于org.springframework.beans.factory.Aware根标记接口。这在应用上下文启动期间使用setter注入来获得一个对象。

The Aware interface is a mix of callback, listener, and observer design patterns. It indicates that the bean is eligible to be notified by the Spring container through the callback methods.

Aware接口是回调、监听器和观察者设计模式的混合。它表明Bean有资格通过回调方法得到Spring容器的通知。

3. BeanNameAware

3、BeanNameAware

BeanNameAware makes the object aware of the bean name defined in the container.

BeanNameAware使对象知道容器中定义的Bean名称

Let’s have a look at an example:

让我们来看看一个例子。

public class MyBeanName implements BeanNameAware {

    @Override
    public void setBeanName(String beanName) {
        System.out.println(beanName);
    }
}

The beanName property represents the bean id registered in the Spring container. In our implementation, we’re simply displaying the bean name.

beanName属性表示在Spring容器中注册的Bean ID。在我们的实现中,我们只是显示了Bean的名字。

Next, let’s register a bean of this type in a Spring configuration class:

接下来,让我们在Spring配置类中注册一个这种类型的Bean。

@Configuration
public class Config {

    @Bean(name = "myCustomBeanName")
    public MyBeanName getMyBeanName() {
        return new MyBeanName();
    }
}

Here we’ve explicitly assigned a name to our MyBeanName class with the @Bean(name = “myCustomBeanName”) line.

在这里,我们用@Bean(name = “myCustomBeanName”)行明确地给我们的MyBeanName类分配了一个名字。

Now we can start the application context and get the bean from it:

现在我们可以启动应用上下文并从中获取Bean。

AnnotationConfigApplicationContext context 
  = new AnnotationConfigApplicationContext(Config.class);

MyBeanName myBeanName = context.getBean(MyBeanName.class);

As we expect, the setBeanName method prints out “myCustomBeanName”.

正如我们所期望的,setBeanName方法打印出“myCustomBeanName”

If we remove the name = “…” code from the @Bean annotation the container, in this case, assigns getMyBeanName()  method name into the bean. So the output will be “getMyBeanName”.

如果我们从@Bean注解中移除name = “…”代码,那么在这种情况下,容器会将getMyBeanName()方法名称分配给Bean。所以输出将是“getMyBeanName”

4. BeanFactoryAware

4、BeanFactoryAware

BeanFactoryAware is used to inject the BeanFactory object. This way we get access to the BeanFactory which created the object.

BeanFactoryAware用于注入BeanFactory对象。这样我们就可以访问到创建该对象的BeanFactory

Here’s an example of a MyBeanFactory class:

下面是一个MyBeanFactory类的例子。

public class MyBeanFactory implements BeanFactoryAware {

    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public void getMyBeanName() {
        MyBeanName myBeanName = beanFactory.getBean(MyBeanName.class);
        System.out.println(beanFactory.isSingleton("myCustomBeanName"));
    }
}

With the help of the setBeanFactory() method, we assign the BeanFactory reference from the IoC container to the beanFactory property.

setBeanFactory()方法的帮助下,我们将IoC容器中的BeanFactory引用分配给beanFactory属性

After that, we can use it directly like in the getMyBeanName() function.

之后,我们可以像在getMyBeanName()函数中那样直接使用它。

Let’s initialize the MyBeanFactory and call the getMyBeanName() method:

让我们初始化MyBeanFactory并调用getMyBeanName()方法。

MyBeanFactory myBeanFactory = context.getBean(MyBeanFactory.class);
myBeanFactory.getMyBeanName();

As we have already instantiated the MyBeanName class in the previous example, Spring will invoke the existing instance here.

由于我们已经在前面的例子中实例化了MyBeanName类,Spring将在这里调用现有的实例。

The beanFactory.isSingleton(“myCustomBeanName”) line verifies that.

beanFactory.isSingleton(“myCustomBeanName”)行验证了这点。

5. When to Use?

5.何时使用?

The typical use case for BeanNameAware could be acquiring the bean name for logging or wiring purposes. For the BeanFactoryAware it could be the ability to use a spring bean from legacy code.

BeanNameAware的典型用例可能是获取Bean名称,用于记录或布线目的。对于BeanFactoryAware来说,它可能是使用遗留代码中的Spring Bean的能力。

In most cases, we should avoid using any of the Aware interfaces, unless we need them. Implementing these interfaces will couple the code to the Spring framework.

在大多数情况下,我们应避免使用任何Aware接口,除非我们需要它们。实现这些接口将使代码与Spring框架耦合。

6. Conclusion

6.结论

In this write-up, we learned about the BeanNameAware and BeanFactoryAware interfaces and how to use them in practice.

在这篇文章中,我们了解了BeanNameAwareBeanFactoryAware接口以及如何在实践中使用它们。

As usual, the complete code for this article is available over on GitHub.

像往常一样,本文的完整代码可以在GitHub上找到