问题描述
我需要从单身得到原型课。我发现方法注入是要去的,但是我并不知道如何使用spring @Lookup注释。我是新来的依赖注入,我选择了注释配置,所以我想继续这个方向。
我发现@Lookup注释最近才被添加(),但我找不到任何地方如何使用它。
所以,在这里是简化的例子
配置类:
@Configuration
@Lazy
public class ApplicationConfiguration实现ApplicationConfigurationInterface {
@Bean
public MyClass1 myClass1(){
return new ContentHolderTabPaneController();
}
@Bean
@Scope(prototype)
public MyClass2 myClass2(){
return new SidebarQuickMenuController();
}
}
这里是类示例:
public class MyClass1 {
doSomething(){
myClass2();
}
//我想要这个方法返回MyClass2原型
public MyClass2 myClass2(){
}
}
如何使用@Lookup注释?
public MyClass2 myClass2()
方法之前,将 @Lookup
注释应用于:
所以删除th e从 ApplicationConfiguration
之后的工厂方法样式bean声明:
@Bean
public MyClass1 myClass1(){
return new ContentHolderTabPaneController();
}
并添加 @Component
注释让Spring实例化bean(也可以在方法中添加 @Lookup
注释):
@Component
public class MyClass1 {
doSomething(){
myClass2();
}
//我想要这个方法返回MyClass2原型
@Lookup
public MyClass2 myClass2(){
return null; //这个实现将被动态生成的子类覆盖
}
}
现在得到 myClass1
bean的上下文,并且它的 myClass2
方法应该被替换/覆盖以获得一个新的原型bean每次。
更新:
使用工厂方法声明
实现 @Lookup
注释方法(查找方法 )。没有 @Lookup
并保持您的配置类不变,现在 MyClass1
看起来像(其实Spring生成一个类似的实现子类如果 @Lookup
被使用):
public class MyClass1 {
doSomething(){
myClass2();
}
//我想要这个方法返回MyClass2原型
@Autowired
private ApplicationContext applicationContext;
public MyClass2 myClass2(){
return applicationContext.getBean(MyClass2.class);
}
}
Spring注入 ApplicationContext
。
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.
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 class:
@Configuration
@Lazy
public class ApplicationConfiguration implements ApplicationConfigurationInterface {
@Bean
public MyClass1 myClass1() {
return new ContentHolderTabPaneController();
}
@Bean
@Scope("prototype")
public MyClass2 myClass2() {
return new SidebarQuickMenuController();
}
}
And here is class example:
public class MyClass1 {
doSomething() {
myClass2();
}
//I want this method to return MyClass2 prototype
public MyClass2 myClass2(){
}
}
How do I do that with @Lookup annotation?
Before applying @Lookup
annotation to your public MyClass2 myClass2()
method, read this in @Lookup's Javadoc:
So remove the following factory method style bean declaration from ApplicationConfiguration
:
@Bean
public MyClass1 myClass1() {
return new ContentHolderTabPaneController();
}
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
}
}
Now get myClass1
bean out of context, and its myClass2
method should have been replaced/overridden to get a new prototype bean each time.
Update:
Using factory method declaration
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 injects the ApplicationContext
for you.
这篇关于如何使用spring @Lookup注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!