As one can see in the documentation of the Spring framework, Spring supports JSR-330 annotations for dependency injection. Auto-wiring of required dependencies is straight forward, @javax.inject.Inject can be used as a drop-in replacement for Springs own @Autowired annotation in that case. However, @Inject does not support the "required" attribute of @Autowired and there is no equivalent in Java 7. Java 8 supports an additional @Optional annotation, but there is nothing that simple in Java 7. However, JSR-330 defines a javax.inject.Provider<T> interface that can be used to achieve optional bean wiring: Instead of auto-wiring the actual bean, you can @Inject a provider to the bean and for example read it out after properties are set, in an init-method or at the time of access to the bean.
Here a short code snippet demonstrating the idea:
package com.example;
import javax.inject.Inject;
import javax.inject.Provider;
public class SomeBean {
@Inject
private Provider<ServiceBean> serviceBeanProvider;
private ServiceBean serviceBean;
private void lookupServiceBean() {
if (serviceBean == null) {
try {
serviceBean = serviceBeanProvider.get();
} catch (Exception e) {
// not found
}
}
}
[...]
}
Spring will wire in the Provider, which throws a NoSuchBeanException if the bean is not found. Again, to keep out all Spring dependencies I'll just catch all exceptions here.
No comments:
Post a Comment