Java BeanNameAware 和 BeanFactoryAware
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19324964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BeanNameAware and BeanFactoryAware
提问by Kanwaljeet Singh
What is the use of BeanNameAware
and BeanFactoryAware
? I was studying spring and came across these two interfaces.
I googled them but nothing useful came up.
Please tell me what is the functionality of BeanNameAware
and BeanFactoryAware
interfaces and when to use these?
有什么用的BeanNameAware
和BeanFactoryAware
?我在学习 spring 时遇到了这两个接口。我用谷歌搜索它们,但没有任何有用的东西出现。请告诉我BeanNameAware
和BeanFactoryAware
接口的功能是什么以及何时使用它们?
采纳答案by nicholas.hauschild
The xxxAware
interface is a common pattern used within the Spring framework. They are typically used to allow a Spring managed bean to be given an object (via the interfaces setXxx
method) at Spring bootstrap time.
该xxxAware
接口是Spring框架内使用的公共模式。它们通常用于允许setXxx
在 Spring 引导时为 Spring 托管 bean 提供对象(通过接口方法)。
Springs documentation says this about the Aware
interface, which is a super interface to the two you mention:
Springs 文档说明了这个Aware
接口,它是您提到的两个接口的超级接口:
Marker superinterface indicating that a bean is eligible to be notified by the Spring container of a particular framework object through a callback-style method.
标记超接口指示 bean 有资格通过回调样式的方法被特定框架对象的 Spring 容器通知。
As Sotirious points out, the Aware
interface has the feel of the listener,?callback, or observer design patterns.
正如 Sotirious 指出的那样,Aware
界面具有侦听器、回调或观察者设计模式的感觉。
Usage would look like this:
用法如下所示:
@Component
public MyBean implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(final BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void myMethod() {
//I can now use beanFactory here
}
}
During bootstrapping, Spring will examine each bean to determine if it implements any of the xxxAware
interfaces. When one is found, it invokes the interface method, providing the piece of information that is being asked for. In the example above, Spring calls MyBean#setBeanFactory
providing its BeanFactory
.
在引导期间,Spring 将检查每个 bean 以确定它是否实现了任何xxxAware
接口。当找到一个时,它调用接口方法,提供所请求的信息。在上面的示例中,Spring 调用MyBean#setBeanFactory
提供其BeanFactory
.
Of course, in many situations, it is not entirely necessary to use these interfaces. For example, the ApplicationContextAware
interface can be circumvented by simply @Autowired
ing an ApplicationContext
into a bean.
当然,在很多情况下,并不是完全需要使用这些接口。例如,ApplicationContextAware
可以通过简单地将@Autowired
anApplicationContext
放入 bean来绕过该接口。
@Component
public class MyOtherBean {
@Autowired
private ApplicationContext applicationContext;
public void someMethod() {
//I can use the ApplicationContext here.
}
}
回答by acsadam0404
BeanNameAware makes the object aware of its bean name. It is best used in pre annotation config spring (2.x). You could reference the bean from a locator by its name then.
BeanFactoryAware gives the bean access to the beanfactory that created it. For the usefulness of this, you should check the documentation: http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/beans/factory/BeanFactoryAware.html
BeanNameAware 使对象知道它的 bean 名称。它最好用于预注释配置 spring (2.x)。然后,您可以通过名称从定位器中引用 bean。
BeanFactoryAware 允许 bean 访问创建它的 beanfactory。对于这个的用处,你应该检查文档:http: //docs.spring.io/spring/docs/2.5.x/api/org/springframework/beans/factory/BeanFactoryAware.html
回答by Marek Switajski
The BeanFactoryAware interface is used during the initialization of an ApplicationContext. It has been used in Spring 2 to customize the weaving of beans before they get initialized. An example would be in order to additionally add pointcuts at load time (LTW) e.g. for autogenerated find methods of DAOs. Another usage could be to load a minimized context for test, to speed up tests (lazy initialization would be a better practice)
BeanFactoryAware 接口在 ApplicationContext 的初始化期间使用。它已在 Spring 2 中用于在 bean 初始化之前自定义 bean 的编织。一个例子是为了在加载时 (LTW) 额外添加切入点,例如用于自动生成的 DAO 查找方法。另一种用法可能是加载最小化的测试上下文,以加快测试速度(延迟初始化是更好的做法)
See also http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-beanfactoryfor the reference.
另请参阅http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-beanfactory以获取参考。
回答by Nisarg Patil
1) BeanFactoryAware
Bean implementing this interface can get the access to BeanFactory
that created it
As, bean implementing this interface can get the current bean factory, this can be used to call any service from the bean factory.
1)BeanFactoryAware
实现这个接口的bean可以访问BeanFactory
创建它的
bean 因为实现这个接口的bean可以获得当前的bean工厂,这可以用来调用bean工厂的任何服务。
2) BeanNameAware
Bean implementing this interface can get its name defined in the Spring container
One possible area of use could be if your building on/ extending the spring framework and would like to acquire the bean name for logging purposes/wiring them etc.
2)BeanNameAware
实现这个接口的 Bean 可以在 Spring 容器中定义它的名称
一个可能的使用领域可能是,如果您在 Spring 框架上构建/扩展,并且想要获取 bean 名称以用于记录目的/连接它们等。