General

How do I implement a custom rule?

The simplest way to implement a custom rule is to subclass net.sourceforge.configured.rules.template.StandardTemplateDependencyResolutionRule.

You must implement the methods:

  • boolean canIdentifyDependency(...) which returns true if your rule matches the dependency
  • void resolveDependency(BeanResolutionInfo contextHolder,...) which should place your resolved object into the BeanResolutionInfo object passed into the method.

    You can also indicate whether the returned object should be autowired using the BeanResolutionInfo.setWireReturnedObject method, by default it will be...


        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);
	}	
}                
       

How do I alter the order that my rules get executed?

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.