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();
// Sorted bean names of every Runnable bean (new in 2.1.0)
Set<String> names = registry.getNames();
// 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) |
| getNames() | Set<String> | Sorted bean names of every Runnable bean, including ancestor bean factories (new in 2.1.0) |
| isNameInUse(name) | boolean | Check if a bean name exists |
| getCount() | int | Total Runnable bean count |
Context Shutdown
Like SpringObjectFactory, the registry releases its reference to the ListableBeanFactory when its container shuts down. Since 2.1.0 both are DisposableBeans that release from their own container's destroy callback (previously a ContextClosedEvent listener), so closing a child context no longer shuts down the parent's registry. Any call to the registry after shutdown throws an UnrulyException with a clear message: "Application Context is closed."