问题描述
我需要从单例中获取原型类.我发现方法注入是要走的路,但我真的不知道如何使用spring @Lookup 注解.
I need to get prototype class from singleton. I found that method injection is the way to go, but I don't really know how to use spring @Lookup annotation.
我是依赖注入的新手,我选择使用注解配置,所以我想继续朝这个方向发展.
I'm new to dependency-injection, and I chose to go with annotation configuration, so I would like to continue in that direction.
我发现@Lookup 注释是最近才添加的(https://spring.io/blog/2014/09/04/spring-framework-4-1-ga-is-here),但我在任何地方都找不到如何使用它.
I found out that @Lookup annotation was added only recently (https://spring.io/blog/2014/09/04/spring-framework-4-1-ga-is-here), but I cannot find anywhere how to use it.
所以,这里是一个简化的例子
So, here is simplified example
配置类:
@Configuration
@Lazy
public class ApplicationConfiguration implements ApplicationConfigurationInterface {
@Bean
public MyClass1 myClass1() {
return new ContentHolderTabPaneController();
}
@Bean
@Scope("prototype")
public MyClass2 myClass2() {
return new SidebarQuickMenuController();
}
}
这是课堂示例:
public class MyClass1 {
doSomething() {
myClass2();
}
//I want this method to return MyClass2 prototype
public MyClass2 myClass2(){
}
}
如何使用 @Lookup 注释来做到这一点?
How do I do that with @Lookup annotation?
推荐答案
在将 @Lookup
注释应用到您的 public MyClass2 myClass2()
方法之前,请阅读 @查找的 Javadoc:
Before applying @Lookup
annotation to your public MyClass2 myClass2()
method, read this in @Lookup's Javadoc:
容器将通过CGLIB生成方法包含类的运行时子类,这就是为什么这样的查找方法只能在容器通过常规构造函数实例化的bean上工作(即查找方法不能被工厂返回的bean替换方法,我们不能为它们动态提供子类).
因此从 ApplicationConfiguration
中删除以下工厂方法样式 bean 声明:
So remove the following factory method style bean declaration from ApplicationConfiguration
:
@Bean
public MyClass1 myClass1() {
return new ContentHolderTabPaneController();
}
并添加@Component
注解让Spring实例化bean(同时添加@Lookup
注解到方法中):
and add @Component
annotation to let Spring instantiate the bean (also add the @Lookup
annotation to the method):
@Component
public class MyClass1 {
doSomething() {
myClass2();
}
//I want this method to return MyClass2 prototype
@Lookup
public MyClass2 myClass2(){
return null; // This implementation will be overridden by dynamically generated subclass
}
}
现在从上下文中获取 myClass1
bean,它的 myClass2
方法应该已经被替换/覆盖以每次都获得一个新的原型 bean.
Now get myClass1
bean out of context, and its myClass2
method should have been replaced/overridden to get a new prototype bean each time.
更新:
实现@Lookup
注释方法(查找方法")并不难.没有 @Lookup
并保持您的配置类不变,现在 MyClass1
看起来像(实际上,如果 @Lookup
使用):
It's not hard to implement the @Lookup
annotated method (the "lookup method"). Without @Lookup
and keeping your configuration class unchanged, now MyClass1
looks like (in fact Spring generates a similar implementation in a subclass if @Lookup
were used):
public class MyClass1 {
doSomething() {
myClass2();
}
//I want this method to return MyClass2 prototype
@Autowired
private ApplicationContext applicationContext;
public MyClass2 myClass2() {
return applicationContext.getBean(MyClass2.class);
}
}
Spring 为您注入 ApplicationContext
.
Spring injects the ApplicationContext
for you.
这篇关于如何使用 spring @Lookup 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!