如何将 bean 注入 Spring Condition 类? [英] How to inject a bean into a Spring Condition class?

查看:50
本文介绍了如何将 bean 注入 Spring Condition 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在定义条件,稍后我将检查这些条件以动态加载我的服务接口的两个实现之一.

I am defining conditions that I will check to dynamically load one of the two implementations of my service interface later.

@Component
public class IsPolicyEnabled implements Condition {

    @Autowired
    private MyProperties props;

    @Override
    public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
        return props.isPolicyEnabled();
    }

}

还有

@Component
public class MyProperties {...}

@Service
@Conditional(IsPolicyEnabled.class)
public class ServiceA implements Service {...}

但是,我遇到了运行时错误.

However, I am running into a runtime error as.

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.NullPointerException
at com.xyz.utils.IsPolicyEnabled.matches(IsPolicyEnabled.java:9)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:88)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:71)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:515)

基本上,它无法初始化已在条件实现中自动连接的道具对象.不允许吗?

Basically, it failed to initialize props object that has been auto-wired inside the condition implementation. Is that not allowed?

如何在条件实现中自动连接另一个依赖项,因为我的条件评估取决于该依赖项提供的值?

How can I auto wire another dependency inside the condition implementation since my condition evaluation depends on a value provided by that dependency?

推荐答案

条件被检查紧接着 bean 定义将被注册 [...]

Conditions are checked immediately before the bean-definition is due to be registered [...]

Condition, Spring Framework 5.0.8.RELEASE API 文档

Condition, Spring Framework 5.0.8.RELEASE API documentation

您不能将 bean 注入 Condition 实例,因为上下文中还没有 bean 定义1.

You can't inject a bean into a Condition instance because there are no bean-definitions in the context yet1.

此外,您不应该在 Condition 类中使用 bean:

Moreover, you are not supposed to work with beans within Condition classes:

条件必须遵循与 BeanFactoryPostProcessor 相同的限制,并且注意不要与 bean 实例交互.

Conditions must follow the same restrictions as BeanFactoryPostProcessor and take care to never interact with bean instances.

Condition, Spring Framework 5.0.8.RELEASE API 文档

Condition, Spring Framework 5.0.8.RELEASE API documentation

你应该重新考虑设计,因为

You should rethink the design because

[...] 我的条件评估取决于该依赖项提供的值.

[...] my condition evaluation depends on a value provided by that dependency.

表示不太正确.

1 准确的说,Spring 已经注册了一些 bean,以满足其自身的需要.

1 Precisely speaking, there are a few beans already registered by Spring for its own needs.

这篇关于如何将 bean 注入 Spring Condition 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆