General
The simplest way to implement a custom rule is to subclass net.sourceforge.configured.rules.template.StandardTemplateDependencyResolutionRule.
You must implement the methods:
public class InstantiationRule extends StandardTemplateDependencyResolutionRule { private Set<Class<?>> intantiatedTypes = new HashSet<Class<?>>(); @Override protected boolean canIdentifyDependency(BeanResolutionInfo contextHolder, Class<?> declaringClass, String fieldName, Class<?> fieldType) { boolean found = false; for(Class<?> clazz : intantiatedTypes) { if(clazz.isAssignableFrom(fieldType)) { found = true; break; } } return found; } @Override protected void resolveDependency(BeanResolutionInfo contextHolder, Class<?> declaringClass, String fieldName, Class<?> fieldType) { try { contextHolder.setObject(fieldType.newInstance()); } catch(Exception e) { throw new BeanGraphAutowiringServiceException(e); } } public void addInstantiatedType(Class<?> interestingClass) { intantiatedTypes.add(interestingClass); } }
Every rule has a precedence property, this is what controls the order in which rules are evaluated. See the example bean definitions on the main page for how the precedence is set.