问题描述
来自我知道当我使用@Bean时,默认值已经等效于:
From this Spring documentation I know that when I use @Bean, the default is already equivalent to:
@Bean(autowire = Autowire.NO)
@Bean(autowire = Autowire.NO)
我只是想了解这对我意味着什么。如果我的系统是100%Java Config 并且没有XML配置,那么据我所知,当我使用@Bean时, Autowire.no不会产生任何影响。
I am just trying to understand what this means for me. If my system is 100% Java Config and has no XML configuration, then from what I can tell, when I use @Bean, the 'Autowire.no' has no impact whatsoever.
编辑
没有影响是指其他@Autowired引用到该bean的是自动装配的(在其他Java Config类中)。我怀疑这是因为在Java Config中没有定义显式的 ref元素,所以此(默认)设置无效。
By "no impact" I mean that other @Autowired references to this bean ARE autowired (in other Java Config classes). I suspect that is because with Java Config there is no explicit 'ref element' defined, so this (default) setting has no effect.
示例:
第一个配置:
package a.b.c;
@Configuration
public class AlphaConfig {
@Bean(autowire = Autowire.NO)
public AlphaBeanType alphaBean() {
return new AlphaBeanType();
}
}
然后在第二个配置中:
package d.e.f;
import a.b.c.AlphaBeanType;
@Configuration
public class AnotherConfig {
@Autowire
private AlphaBeanType alphaBeanType;
@Bean
. . .
}
我看到的是'alphaBeanType'总是自动连接到第二个配置类中-似乎与文档冲突-因此是我的问题。
What I see is that 'alphaBeanType' is always autowired in the second config class - which seems to be in conflict with the documentation - hence my question.
最终编辑
当然,我不能完全从文档中得知!有人确定吗?
Of course, I can't quite tell from the documentation! Does anyone know for sure?
推荐答案
设置 Autowire.NO
不会表示该豆不能通过 @Autowire
注入其他豆中。 @Autowire
默认情况下按类型工作,也可以使用 @Qualifier
按名称工作。
Setting Autowire.NO
does not mean that the bean cannot be injected in other beans via @Autowire
. @Autowire
works by default by type, and can also work by name using @Qualifier
.
因此,如果您的bean具有正确的类型或名称,它将被注入其他bean中,这是正常的。
So if your bean has the right type or name, it will get inject in other beans, that's normal.
Autowire.NO
的含义类似于:
这是一个如何工作的代码示例,让我们定义两个bean:
This a code example of how this works, let's define two beans:
public class MyBeanTwo {
public MyBeanTwo() {
System.out.println(">>> MY Bean 2 created!");
}
}
public class MyBean {
private MyBeanTwo myBeanTwo;
public MyBean() {
System.out.println(">>>MyBean created !!");
}
public void setMyBeanTwo(MyBeanTwo myBeanTwo) {
System.out.println(">>> Injecting MyBeanTwo INTO MyBeanOne !!!");
this.myBeanTwo = myBeanTwo;
}
}
和某些配置:
@Configuration
public class SimpleConfigOne {
@Bean
public MyBean createMyBean() {
return new MyBean();
}
@Bean
public MyBeanTwo createMyBeanTwo() {
return new MyBeanTwo();
}
}
使用此配置,此应用程序的启动将为日志:
With this configuration, the startup of this application gives this log:
>>>MyBean created !!
>>> MY Bean 2 created!
每个Bean均创建了一个实例,但 MyBean
没有注入 MyBeanTwo
,即使存在具有正确类型的强韧豆。
Meaning one instance of each bean was created, but MyBean
did NOT get injected with MyBeanTwo
, even tough a bean with the correct type existed.
通过声明 MyBean
像这样:
@Bean(autowire = Autowire.BY_TYPE)
public MyBean createMyBean() {
return new MyBean();
}
MyBeanOne
现在可以通过按类型自动装配来设置其属性。
MyBeanOne
is now eligible to have it's properties set via autowiring by type.
启动日志变为:
>>>MyBean created !!
>>> MY Bean 2 created!
>>> Injecting MyBeanTwo INTO MyBeanOne !!!
这表明 MyBean
的 MyBeanTwo
通过按类型注入按类型进行注入。
This shows that MyBean
had MyBeanTwo
injected by type via a by type injection.
为什么使用Autowire.NO是默认值:
通常,我们不想自动连接使用 @Bean
创建的bean的属性。我们通常要做的是通过代码显式设置属性以提高可读性,作为一种文档形式,并确保将属性设置为正确的值。
Usually we don't want to autowire the properties of beans created with @Bean
. What we usually do is set the properties explicitly via code for readability, as a form of documentation and to make sure the property is set with the correct value.
这篇关于在春季,'autowire = Autowire.NO'是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!