简体   繁体   English

如何在拥有对象(也就是@Autowired)上的注释条件下为@Autowire字段提供其他bean?

[英]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.

相关问题 如何基于注释为@Autowire字段提供不同的bean实现? - How provide a different bean implementation to @Autowire field based on annotation? 无法自动连线栏位@Autowired注解 - Could not autowire field @Autowired Annotation 没有注释的自动装配@Autowired - Autowire without annotation @Autowired 如何以编程方式自动连接不属于bean的字段? - How can I programmatically autowire field which is not part of bean? 创建bean时出错,自动连接依赖项注入失败,无法自动连接字段 - Error creating bean, Injection of autowired dependencies failed, Could not autowire field Spring @Autowired 不工作 - 预计至少有 1 个 bean 有资格作为 autowire 候选 - Spring @Autowired not working - expected at least 1 bean which qualifies as autowire candidate 当我在配置文件中有bean定义以及使用@Autowire注释时,将使用哪个对象 - Which object will be used when I have bean definition in config file as well as using @Autowire annotation 如何在bean上提供@ControllerAdvice条件? - How to provide a @ControllerAdvice conditional on bean? @Autowire 如何在不使用 @Bean 注释的情况下获取 spring bean - How @Autowire gets the spring bean without using @Bean annotation 如何在 Spring 中定义要自动装配的 bean - How to define which bean to autowire in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM