The AutowiredAnnotationBeanFactoryPostProcessor
(Spring 3.2) contains this method to determine if a supported 'Autowire' annotation is required or not:
/**
* Determine if the annotated field or method requires its dependency.
* <p>A 'required' dependency means that autowiring should fail when no beans
* are found. Otherwise, the autowiring process will simply bypass the field
* or method when no beans are found.
* @param annotation the Autowired annotation
* @return whether the annotation indicates that a dependency is required
*/
protected boolean determineRequiredStatus(Annotation annotation) {
try {
Method method = ReflectionUtils.findMethod(annotation.annotationType(), this.requiredParameterName);
if (method == null) {
// annotations like @Inject and @Value don't have a method (attribute) named "required"
// -> default to required status
return true;
}
return (this.requiredParameterValue == (Boolean) ReflectionUtils.invokeMethod(method, annotation));
}
catch (Exception ex) {
// an exception was thrown during reflective invocation of the required attribute
// -> default to required status
return true;
}
}
In short, no, not by default.
The method name that is being looked for by default is 'required', which is not a field on the @Inject
annotation, thus, method
will be null
and true
will be returned.
You may be able to change that by subclassing this BeanPostProcessor
and overriding the determineRequiredStatus(Annotation)
method to return true, or rather, something 'smarter'.