05 — Advanced

Rule Registry

The SpringRuleRegistry is a registry backed by the Spring application context. Instead of maintaining its own internal collection, it delegates directly to ListableBeanFactory — your rules are Spring beans, and the registry queries them as such.

Injecting the Registry

The registry is auto-configured and available for injection:

@Service
public class RuleExecutionService {

    private final RuleRegistry registry;

    @Autowired
    public RuleExecutionService(RuleRegistry registry) {
        this.registry = registry;
    }

    public void executeByName(String ruleName) {
        Rule rule = registry.get(ruleName);
        rule.run();
    }
}

Looking Up Rules

You can retrieve a rule by its name (the bean name assigned during scanning):

// Get a rule by name
Rule rule = registry.get("CreditCheckRule");

// Get with a specific type
Rule rule = registry.get("CreditCheckRule", Rule.class);

Listing All Rules

// All registered rules
List<Rule> rules = registry.getRules();

// All registered rulesets
List<RuleSet> ruleSets = registry.getRuleSets();

// Check if a name is already registered
boolean exists = registry.isNameInUse("CreditCheckRule");

// Total count of runnable beans
int count = registry.getCount();

Registry API Reference

Method Returns Description
get(name) T Retrieve a rule/ruleset by bean name
get(name, type) T Retrieve with type safety
getRules() List<Rule> All Rule beans in the context
getRuleSets() List<RuleSet> All RuleSet beans in the context
isNameInUse(name) boolean Check if a bean name exists
getCount() int Total Runnable bean count

Context Shutdown

Like SpringObjectFactory, the registry listens for ContextClosedEvent and releases its reference to the ListableBeanFactory. Any call to the registry after shutdown throws an UnrulyException with a clear message: "Application Context is closed."