我正在学习 Spring,据我了解,当我们在具有通用名称的方法(不是 setter 方法)上使用 @annotation 时,方法的参数是自动装配的.
I am learning Spring, and as far as I understand, when we use @annotation on a method which has a generic name (not a setter method), then the method's arguments are autowired.
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog mC,
CustomerPreferenceDao cPD) {
this.movieCatalog = mC;
this.customerPreferenceDao = cPD;
}
// ...
}
因此,在此处,movieCatalog 和 customerPreferenceDao 字段自动使用 mC 和 cPD 的值.我不明白的是,这与没有@autowired"的相同方法有何不同.
So, here, the fields movieCatalog and customerPreferenceDao are autowired with the values of mC and cPD. What I fail to understand is how is this different from the same method without the "@autowired".
我理解 @autowired 应用于字段名称时,但无法理解值何时显式传递给方法(setter 或任何其他方法),那么 Spring 有什么特别之处?
I understand @autowired when applied to a Field name, but not able to understand when the values are explicitly being passed to the method (either a setter or any other method), then what does Spring do special?
答案很晚,但这里是:
任何用 @Autowired 注释的方法都是配置方法.字段注入完成后,在 bean 实例化时调用它.方法的参数在调用时注入到方法中.
any method annotated with @Autowired is a config method. It is called on bean instantiation after field injection is done. The arguments of the method are injected into the method on calling.
这篇关于@autowired on 方法在 Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)