英文:
How force spring to inject from not named implementation
问题
我正在使用Atlassian SDK来创建一个插件。SDK中有一个名为FieldHtmlFactory
的接口,SDK中的一个名为fieldHtmlFactory
的类已对其进行了实现,因此我们可以使用注入来访问它们。但由于某些原因,在某些情况下,我应该对这个接口进行另一个实现。现在我可以通过类名注入自己的类,但由于SDK实现没有任何名称,我不能再使用它了。是否有一种方法可以强制Spring在这种情况下使用SDK的实现?
SDK接口:
@ExperimentalApi
public interface FieldHtmlFactory {
List<FieldHtmlBean> getCreateFields(ApplicationUser var1, OperationContext var2, Action var3, MutableIssue var4, boolean var5, List<String> var6);
List<FieldHtmlBean> getLinkedIssueCreateFields(ApplicationUser var1, OperationContext var2, Action var3, MutableIssue var4, Issue var5, boolean var6, List<String> var7);
List<FieldHtmlBean> getEditFields(ApplicationUser var1, OperationContext var2, Action var3, Issue var4, boolean var5);
List<FieldHtmlBean> getInlineEditFields(ApplicationUser var1, OperationContext var2, Action var3, Issue var4, boolean var5);
List<FieldHtmlBean> getSubTaskCreateFields(ApplicationUser var1, OperationContext var2, Action var3, MutableIssue var4, boolean var5, List<String> var6);
}
SDK实现:
public class fieldHtmlFactory implements FieldHtmlFactory {
...
}
我的实现:
@Named("FieldEditorFactoryImpl")
public class EditorHtmlFactoryImpl implements FieldHtmlFactory {
...
}
在某处使用我的类:
@Autowired
public MyREST(@Named("FieldEditorFactoryImpl") FieldHtmlFactory fieldHtmlFactory, ...)
我需要像这样使用SDK类,但是这样做不起作用:
@Autowired
public MyREST(@Named("") FieldHtmlFactory fieldHtmlFactory, ...)
英文:
I am using the Atlassian SDK to create a plugin. There is an interface named FieldHtmlFactory
in the SDK which a class named fieldHtmlFactory
has implemented it so we can use injection to access them. But for some reason, I should make another implementation in some cases from this interface. Now I can inject my own class by its name but as SDK implementation has not any name I cannot use it anymore. Is there a way to force spring to use SDK's implementation in this case?
SDK interface:
@ExperimentalApi
public interface FieldHtmlFactory {
List<FieldHtmlBean> getCreateFields(ApplicationUser var1, OperationContext var2, Action var3, MutableIssue var4, boolean var5, List<String> var6);
List<FieldHtmlBean> getLinkedIssueCreateFields(ApplicationUser var1, OperationContext var2, Action var3, MutableIssue var4, Issue var5, boolean var6, List<String> var7);
List<FieldHtmlBean> getEditFields(ApplicationUser var1, OperationContext var2, Action var3, Issue var4, boolean var5);
List<FieldHtmlBean> getInlineEditFields(ApplicationUser var1, OperationContext var2, Action var3, Issue var4, boolean var5);
List<FieldHtmlBean> getSubTaskCreateFields(ApplicationUser var1, OperationContext var2, Action var3, MutableIssue var4, boolean var5, List<String> var6);
}
SDK implementation:
public class fieldHtmlFactory implement FieldHtmlFactory{
...
}
My implementaion:
@Named("FieldEditorFactoryImpl")
public class EditorHtmlFactoryImpl implement FieldHtmlFactory{
...
}
Use somewhere my class:
@Autowired
public MyREST(@Named("FieldEditorFactoryImpl") FieldHtmlFactory fieldHtmlFactory, ...)
I need something like this to use SDK class, but, it dosen't work:
@Autowired
public MyREST(@Named("") FieldHtmlFactory fieldHtmlFactory, ...)
答案1
得分: 1
好的,以下是已经翻译好的部分:
好的,所以有两种不同的bean,一种是SDK提供的,可能由一些自动配置定义,另一种是你自己定义的。
将SDK提供的bean设置为@Primary bean会很有用,但你需要准确地查看它是如何定义的。通常它应该使用@ConditionalOnMissingBean进行注释,这样你就可以自己定义两种不同的bean,但显然在你的情况下并非如此。
一个简单的解决方法是使用它的默认名称,这个默认名称是类的简单名称。
@Autowired
public MyREST(@Named("fieldHtmlFactory") FieldHtmlFactory fieldHtmlFactory, ...)
英文:
OK, so there are two different beans, the one provided by the SDK, probably defined by some auto-configuration, and your own.
It would be useful to have the SDK one as a @Primary bean, but you'll need to see exactly how it's defined. Normally it schould be annotated with @ConditionalOnMissingBean, so you could define two different ones yourself, but it apparently is not in your case.
A dirty fix is to use it's default name, which is the simple class name.
@Autowired
public MyREST(@Named("fieldHtmlFactory") FieldHtmlFactory fieldHtmlFactory, ...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论