@autowired on Spring中的方法 [英] @autowired on method in Spring

查看:69
本文介绍了@autowired on Spring中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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应用于字段名称时,我会理解@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中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆