可以在JSR 330中使@Inject成为可选项(如@Autowire(required = false)吗?
Spring的@Autowire
可以配置为如果找不到匹配的autowire候选者,Spring不会抛出错误: @Autowire(required=false)
是否有等效的JSR-330注释? 如果没有匹配的候选者,@ @Inject
总是会失败。 有没有什么方法可以使用@Inject
但如果没有找到匹配的类型,框架是否会失败? 我找不到那种程度的文件。
不……在JSR 330中没有可选的等价物…如果你想使用可选注入,那么你必须坚持使用特定于框架的@Autowired
注释
您可以使用java.util.Optional
。 如果您使用的是Java 8,而您的Spring版本是4.1
或更高版本(请参阅此处 ),而不是
@Autowired(required = false) private SomeBean someBean;
您可以使用Java 8附带的java.util.Optional
类。 使用它像:
@Inject private Optional someBean;
此实例永远不会为null
,您可以像以下一样使用它:
if (someBean.isPresent()) { // do your thing }
这样你也可以做构造函数注入,需要一些bean和一些bean可选,提供了很大的灵活性。
注意:不幸的是,Spring不支持Guava的com.google.common.base.Optional
(请参见此处 ),因此只有在使用Java 8(或更高版本)时此方法才有效。
实例注入经常被忽视。 它增加了很大的灵活性 在获取依赖项之前检查依赖项的可用性。 不满意的获得将抛出一个昂贵的例外。 使用:
@Inject Instance instance; SomeType instantiation; if (!instance.isUnsatisfied()) { instantiation = instance.get(); }
您可以正常限制注射候选者:
@Inject @SomeAnnotation Instance instance;
AutowiredAnnotationBeanFactoryPostProcessor
(Spring 3.2)包含此方法以确定是否需要支持的“Autowire”注释:
/** * Determine if the annotated field or method requires its dependency. * 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; } }
简而言之,不,不是默认。
默认情况下要查找的方法名称是’required’,它不是@Inject
批注中的字段,因此, method
将为null
并返回true
。
您可以通过inheritance此BeanPostProcessor
并覆盖determineRequiredStatus(Annotation)
方法来更改true,或者更确切地说,更“智能”。
可以创建一个可选的注射点!
您需要使用注释查找,如http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html#lookup中所述。
@Inject Instance instance; // In the code try { instance.get(); }catch (Exception e){ }
甚至是所有类型的实例
@Inject Instance> instances
如果需要,get()方法也是惰性求值。 默认注入在启动时评估,如果没有找到可以注入的bean则抛出exception,当然会在运行时注入bean,但如果不可能,应用程序将无法启动。 在文档中,您将找到更多示例,包括如何过滤注入的实例等等。