spring @Autowired 注释应该在哪里 - 在属性或方法上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3746574/
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-09-08 04:08:47  来源:igfitidea点击:

Where is the @Autowired annotation supposed to go - on the property or the method?

springdependency-injectionspring-mvcannotationsautowired

提问by Thom Wilkie

Which is more correct?

哪个更正确?

This (with the @Autowired annotation on the method)?

这(在方法上带有@Autowired 注释)?

@Controller
public class MyController
{
    private MyDao myDao;

    @Autowired
    public MyController(MyDao myDao)
    {
        this.myDao = myDao;
    }

This (with the @Autowired annotation on the property)?

这(在属性上带有@Autowired 注释)?

@Controller
public class MyController
{
    @Autowired
    private MyDao myDao;

    public MyController(MyDao myDao)
    {
        this.myDao = myDao;
    }

Where is the @Autowired annotation supposed to go?

@Autowired 注释应该去哪里?

回答by NamshubWriter

According to the Javadoc for Autowired, the annotation can be used on "a constructor, field, setter method or config method". See the full documentationfor more details.

根据Autowired 的 Javadoc,注释可用于“构造函数、字段、setter 方法或配置方法”。有关更多详细信息,请参阅完整文档

I personally prefer your first option (constructor injection), because the myDaofield can be marked as final:

我个人更喜欢您的第一个选项(构造函数注入),因为该myDao字段可以标记为 final:

@Controller
public class MyControllear {
    private final MyDao myDao;

    @Autowired
    public MyController(MyDao myDao) {
      this.myDao = myDao;
    }

Constructor injection also allows you to test the class in a unit test without code that depends on Spring.

构造函数注入还允许您在单元测试中测试类,而无需依赖 Spring 的代码。

The second option would be better written as:

第二个选项最好写成:

@Controller
public class MyControllear {
    @Autowired
    private MyDao myDao;

    MyController() {
    }

With field injection, Spring will create the object, then update the fields marked for injection.

通过字段注入,Spring 将创建对象,然后更新标记为注入的字段。

One option you didn't mention was putting @Autowiredon a setter method (setter injection):

您没有提到的一个选项是@Autowired使用 setter 方法(setter 注入):

@Controller
public class MyControllear {
    private MyDao myDao;

    MyController() {
    }

    @Autowired
    public void setMyDao(MyDao myDao) {
      this.myDao = myDao;
    }

You do not have to choose one or another. You can use field injection for some dependencies and constructor injection for others for the same object.

您不必选择其中之一。您可以对某些依赖项使用字段注入,对同一对象的其他依赖项使用构造函数注入。

回答by dj_segfault

The annotation goes with the property, because that's what's being autowired; the property to be automatically set. This tutorialhas a nice example. This more advanced exampleshows how to use qualifiers to disambiguate the wiring.

注释与属性一致,因为这是自动装配的;要自动设置的属性。 本教程有一个很好的例子。 这个更高级的示例展示了如何使用限定符来消除接线歧义。