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;
private final RuleContextOptions ruleContextOptions;
@Autowired
public RuleExecutionService(RuleRegistry registry, RuleContextOptions ruleContextOptions) {
this.registry = registry;
this.ruleContextOptions = ruleContextOptions;
}
public void executeByName(String ruleName) {
// Preferred: run through a RuleFlow; the registry lookup happens at execution time
RuleFlow<RuleContext> flow = RuleFlow.builder()
.name("ExecuteByNameFlow")
.run(ruleName)
.build();
flow.run(RuleContext.builder().with(ruleContextOptions).build());
}
}
Building the context with the auto-configured RuleContextOptions bean wires the Spring registry into the flow, so run(name) and run(Class) lookups resolve against your Spring beans at execution time.
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();
// All registered ruleflows (new in 2.0.0)
RuleFlow<?> flow = registry.getRuleFlow("CheckoutFlow");
List<RuleFlow<?>> flows = registry.getRuleFlows();
// 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 |
| getRuleFlow(name) | RuleFlow<?> | Retrieve a RuleFlow bean by name (new in 2.0.0) |
| getRuleFlows() | List<RuleFlow<?>> | All RuleFlow beans in the context (new in 2.0.0) |
| 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."