[英]How provide a different bean to an @Autowire field conditional on an annotation on the owning object which is also @Autowired?
I have a config class that provides two implemenations of the same base bean interface. 我有一个配置类,提供了相同基本bean接口的两个实现。 I want these to be used conditionally based on what annotation the owning class was autowired with.
我希望根据拥有类自动装配的注释来有条件地使用它们。
The usage of the "owning" class: “拥有”类的用法:
public class MyController
{
@Autowired
private OwnerInterface baseOwner;
@Autowired
@MyAnnotation
private OwnerInterface specialOwner;
}
The owning class: 所属类:
public class OwningClass implements OwnerInterface
{
//This is the one I want to supply a conditional bean for
@Autowired
private MyBeanInterface someBean;
}
This is a pesudo-code of the config class: 这是config类的伪代码:
@Configuration
public class ConfigClass
{
@Bean
//Should I use a different conditional?
//And if I make a static method to use here, how would I pass the owning class to it?
@ConditionalOnExpression(...elided...)
public MyBeanInterface getNormalBeanInterface()
{
return new MyBeanInterfaceImpl();
}
@Bean
@ConditionalOnExpression(...elided.../* MyAnnotation */)
public MyBeanInterface getSpecialBeanInterface()
{
return new MyBeanInterfaceForMyAnnotation();
}
}
For the conditional, is there a way to pass the owning object to it? 对于条件,是否有办法将拥有的对象传递给它? I would use a static method like:
我将使用静态方法,例如:
@ConditionalOnExpression(
"#{T(MyAnnotationVerifier).isAnnotatedBy(ownignObject))}"
)
Is there a better solution? 有更好的解决方案吗?
I don't want to use AOP because that's during use of the application and adds overhead to every call. 我不想使用AOP,因为那是在使用应用程序期间,并且增加了每个调用的开销。 If I can instead provide the bean, then it will happen at startup when the objects are created.
如果我可以提供bean,那么它将在启动时创建对象。
Can I use a different @Conditional
annotation to do this? 我可以使用其他
@Conditional
注释执行此操作吗?
If the decision has to be made statically (like your @MyAnnotation), you can alternatively use @Primary 如果必须静态决定(例如@MyAnnotation),则可以使用@Primary
@Bean
@Primary
public MyBeanInterface getNormalBeanInterface()
{
return new MyBeanInterfaceImpl();
}
@Bean
public MyBeanInterface getSpecialBeanInterface()
{
return new MyBeanInterfaceForMyAnnotation();
}
This way, you can auto-wire by Type and it will always refer to the bean with @Primary 这样,您可以按类型自动连线,它将始终使用@Primary引用bean。
Alternative to this: use auto-wire by name 替代方法:按名称使用自动连线
public class ConfigClass {
@Bean
public MyBeanInterface normalBean(){
return new MyBeanInterfaceImpl();
}
@Bean
public MyBeanInterface specialBean(){
return new MyBeanInterfaceForMyAnnotation();
}
}
public class MyController {
@Autowired
private OwnerInterface normalBean;
@Autowired
private OwnerInterface specialBean;
}
To Note here: the variable names ( specialBean
/ normalBean
) here in MyController
match exactly with the @Bean methods in ConfigClass
这里要注意:变量名(
specialBean
/ normalBean
)在这里MyController
比赛正好与在@Bean方法ConfigClass
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.