Skip to content

How to make Spring automatically manage and inject dependencies into rule classes defined via annotations (e.g., Easy Rules' @Rule)? Tried multiple approaches, but injection always results in null. #434

@ericshang2019

Description

@ericshang2019

@component
@rule(name = "ConnectorInfoNonNullCheckRule", description = "联系人校验(依赖前序规则)", priority = 3)
public class ConnectorInfoNonNullCheckRule {
@resource
private BatchCreateService batchCreateActivityService;

@Condition
public boolean shouldCheck(Facts facts) Boolean passed) {
 
}

@Action
public void validate(Facts facts) {
   
}

}

@bean
public Rules rules() throws Exception {
Rules rules = new Rules();

    // 扫描带有 @Rule 注解的类
    ClassPathScanningCandidateComponentProvider scanner =
            new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Rule.class));

    Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents("cn.com.autohome.tuan.easyrules");

    // 动态注册所有规则
    for (BeanDefinition beanDefinition : beanDefinitions) {
        Class<?> ruleClass = Class.forName(beanDefinition.getBeanClassName());
        rules.register(ruleClass.getDeclaredConstructor().newInstance());
    }

    return rules;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions