Java @Autowired 与 @Required 在 setter 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18884670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:53:52  来源:igfitidea点击:

@Autowired vs @Required on setter

javaspringresourcesautowiredrequired

提问by 0x56794E

I'm curious to know what's the difference between code like this:

我很想知道这样的代码有什么区别:

class MyClass {
   @Autowired
   MyService myService;
}

and code like this:

和这样的代码:

class MyClass {
   MyService myService;

   @Required
   public void setMyService(MyService val) {
       this.myService = val;
   }
}

采纳答案by Sudarshan_SMD

@Autowiredannotation is used when you want to autowire a bean. @Autowiredis not limited to setter. It can be used with a constructor and a field as well. If you use @Autowiredannotation on a field, that field will be autowired with bean having matching data type.

@Autowired当您想要自动装配 bean 时使用注释。@Autowired不限于二传手。它也可以与构造函数和字段一起使用。如果您@Autowired在字段上使用注释,则该字段将使用具有匹配数据类型的 bean 自动装配。

@Requiredchecks if a particular property has been set or not. If a field has been annotated with @Requiredannotation and that field is not set, you will get org.springframework.beans.factory.BeanInitializationException.

@Required检查是否已设置特定属性。如果某个字段已使用@Requiredannotation 进行注释并且该字段未设置,则您将获得org.springframework.beans.factory.BeanInitializationException.

Refer:

参考:

Spring @Autowired usage

Spring @Autowired 用法

Recommended usage of Spring's @Required annotation

Spring的@Required注解的推荐用法

Edit: As pointed by 'kryger': field annotated with @Autowiredis effectively also @Required(unless you explicitly set its parameter required to false). eg:

编辑:正如 'kryger' 所指出的:用 注释的字段@Autowired也是有效的@Required(除非您明确地将其参数 required 设置为 false)。例如:

@Autowired(required=false)
private ObjectType objectType;

For a field that has been annotated @Autowired, if bean with matching data type in not available, org.springframework.beans.factory.BeanCreationExceptionis thrown.

对于已注解的字段@Autowired,如果匹配数据类型的 bean 不可用,org.springframework.beans.factory.BeanCreationException则抛出。

回答by watchme

@Autowiredis not the same as @Required.

@Autowired不一样@Required

The @Autowire-Annotation(as in your code-example), tells the ApplicationContext (a.k.a the Spring-IoC-Containter) to inject the desired dependency. (No matter how, if its by using annotations or the XML-File of the ApplicationContext).

@Autowire-Annotation(在你的代码示例),讲述的ApplicationContext(又名春-IOC-Containter)注入所需的依赖。(无论如何,如果它通过使用注释或 ApplicationContext 的 XML 文件)。

The @Required-Annotation, tells the ApplicationContext that this property has to be mentioned in the XML-file (The XML-File of the ApplicationContext), which than leds to the dependency being injected by using the XML-File (or to an expection of course). But the Annotation on its own doesn't tell to inject the dependency! The injection is done because the property is mentioned in the XML-file.That's good to know, you may need it.

@Required-Annotation,讲述的ApplicationContext这个属性在XML的文件(在ApplicationContext的XML的文件),其比LED来的依赖被使用XML的文件注入被提及(或过程的厚望)。但是 Annotation 本身并没有告诉注入依赖项! 注入完成是因为 XML 文件中提到了该属性。很高兴知道,您可能需要它。

With mentioning the property in a XML-File I mean such a configuration for instance:

提到 XML 文件中的属性,我的意思是这样的配置,例如:

<bean id="MyClass" class="com.myclasses.common.MyClass">
     <property name="someProperty" value="ValueThatHasToBeInjected" />
</bean>

So why should I use it over the @Autowired-Annotation?

那么我为什么要在@Autowired-Annotation 上使用它呢?

You should use it when the dependency has to be injected by the information in the XML-configuration file.

当必须通过 XML 配置文件中的信息注入依赖项时,您应该使用它。

Can you give me an example?

你能给我一个例子吗?

Well, there is already a very good example on this website.where this is also explained.

嗯,这个网站上已经有一个很好的例子了。这也解释了。