[英]Difference between @Autowired and @Required with setter injection in Spring
I know a lot had been written about @Autowired
and @Required
annotations. 我知道关于
@Autowired
和@Required
批注的文章很多。
But I have a very basic question as below - 但是我有一个非常基本的问题,如下所示:
What is the difference between the below two mechanism of setter injection. 以下两种二传手注射机制有什么区别。
I haven't yet got a complete satisafactory answer for this. 我还没有一个令人满意的完整答案。
The reason is : 原因是 :
1) @Autowired
without any extra information like - @Autowired(required=false)
is same as @Required
1)
@Autowired
没有任何其他信息,例如-@ @Autowired(required=false)
与@Required
相同
2)What are we gaining from @Required
additionally ? 2)我们还能从
@Required
获得什么?
3)Why @Required
is recommend over @Autowired
on setter injection ? 3)为什么在二传手注射时推荐
@Required
优于@Autowired
?
class MyClass {
private MyService myService;
@Autowired
public void setMyService(MyService val) {
this.myService = val;
}
}
class MyClass {
private MyService myService;
@Required
public void setMyService(MyService val) {
this.myService = val;
}
}
@Autowired
is not the same as @Required
. @Autowired
与@Required
。
The @Required
-Annotation is specialized for telling Spring that this property has to be injected by the information given in the XML-configuration-file (eager) and not through annotations. @Required
-Annotation专门用于告诉Spring该属性必须由XML配置文件(急切)中给出的信息注入,而不是通过注释注入。 And that doesn't matter when you use the @Autowire
-Annotation. 当您使用
@Autowire
-Annotation时,这并不重要。
The @Autowire
-Annotation (as in your code-example), tells the ApplicationContext (aka the Spring-IoC-Containter) to inject the desired dependency. @Autowire
-Annotation (如您的代码示例中所示),告诉ApplicationContext(又名Spring-IoC-Containter)注入所需的依赖项。 (No matter how, if its by using annotations or the XML-File of the ApplicationContext). (无论如何,是否通过使用批注或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), but the Annotation on its own doesn't tell to inject the dependency. @Required
-Annotation告诉ApplicationContext必须在XML文件(ApplicationContext的XML文件)中提及此属性,但是Annotation本身并不能告诉您注入依赖项。 So it is used to check if it is in the XML-configuration file, but not to inject a dependency. 因此,它用于检查它是否在XML配置文件中,而不用于注入依赖项。 The injection is done because the property is mentioned in the XML-file.
因为在XML文件中提到了属性,所以完成了注入。
So in the end it tells that the injection has to be done because of a configuration in the XML-File. 因此最后,它表明由于XML文件中的配置而必须进行注入。 But again: The annotation doesn't tell that the dependency has to be injected, but that it has to be mentioned in the XML-File - which then lets the dependency be injected.
但是再一次:注释并没有说明必须注入依赖项,而是必须在XML-File中提及它-然后允许注入依赖项。
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 due to the informatoin given 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.
在此也进行了解释。
1) You can think of @Required
as a check that the property has been eagerly initialised. 1)您可以将
@Required
视为该属性已被初始化的检查。 In other words it requires that it's been injected via configuration (xml or annotation). 换句话说,它要求通过配置(xml或注释)注入它。 If annotation is used then you'll see it alongside
@Autowired
. 如果使用了注释,那么您会在
@Autowired
旁边看到它。 If the Bean injected does not exist the application fails to startup with a runtime exception. 如果注入的Bean不存在,则应用程序将因运行时异常而无法启动。
2) Nothing more nothing less. 2)没事没事。
@Required
is very specific in what it's meant to be: a) only applicable on methods, b) requires the bean or else application runtime error on startup. @Required
的含义非常具体:a)仅适用于方法,b)在启动时需要bean或其他应用程序运行时错误。 Again, you need dependency injection either via annotation or xml. 同样,您需要通过注释或xml进行依赖项注入。
3) Most likely you want to know at startup if a Bean is failed to be injected and for that reason you can have @Required
along with @Autowired
for expressiveness. 3)最有可能在启动时想知道是否无法注入Bean,因此您可以将
@Required
和@Autowired
以提高表达力。 Functionality-wise you don't need it if you have @Autowired
. 从功能
@Autowired
,如果您具有@Autowired
,则不需要它。
Extra notes: 额外说明:
@Autowired
has more functionality on the other side of the coin that is - when you want to achieve laziness. @Autowired
在硬币的另一侧具有更多功能,即当您要实现懒惰时。 So on a setter method: 所以在setter方法上:
@Autowired(required = false)
@Autowired(required = false)
@Lazy
@Lazy
配对 @Autowired
(without required = false) on a setter method whose param is Java 8's Optional achieving the same effect. @Autowired
(无需required = false)来达到相同的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.