简体   繁体   English

使用注解的构造方法注入

[英]Constructor injection using Annotations

Why my code is failing with the below error when I AUTOWIRE the no arg constructor but it runs fine when I autowire only the single arg constructor 为什么当我自动连接no arg构造函数时,我的代码失败并显示以下错误,但是当我仅自动装配单个arg构造函数时,它运行良好

Exception in thread "main" java.lang.NullPointerException 线程“主”中的异常java.lang.NullPointerException

Here is TennisCoach Class code snippet 这是TennisCoach课程代码段

@Component // this is bean id 
public class TennisCoach implements Coach {

    public TennisCoach(FortuneService thefortuneservice) {
        System.out.println(" inside 1 arg  constructter");
        fortuneservice = thefortuneservice;
    }


    @Autowired
    public TennisCoach() {
        System.out.println(" inside  0 arg constructter");

    }
}

Coach theCoach = myapp.getBean("tennisCoach", Coach.class);
System.out.println(theCoach.getDailyFortunes());

How are things working behind the scene? 事情在幕后如何运作?

If you have only one constructor in a class, you don't need to use @Autowired --Spring understands that there's only one option, so it uses that one. 如果一个类中只有一个构造函数,则无需使用@Autowired --Spring知道只有一个选项,因此它使用了那个。 But you have two constructors, so you need to tell Spring which one to use. 但是您有两个构造函数,因此您需要告诉Spring使用哪个构造函数。

When you invoke the default constructor, however, what do you expect to happen? 但是,当您调用默认构造函数时,您期望发生什么? You don't set up your variables anywhere at all, but you are trying to read from them. 您根本不会在任何地方设置变量,但是您正在尝试从中读取变量。

Constructor with no arguments doesn't want you put @Autowired on it as you are not injecting anything in its arguments (since there are none). 没有参数的构造函数不希望您在其上放置@Autowired,因为您没有在其参数中注入任何内容(因为没有参数)。 @Autowired is required for constructor with arguments however. 但是,带参数的构造函数需要@Autowired。

@Component annotation above the class will create your bean via calling the default constructor and you can use that anywhere but need to make sure that you don't call anything on the fortuneservice as that will result in a null ptr exception unless you initialize it. 类上方的@Component批注将通过调用默认构造函数来创建您的bean,您可以在任何地方使用它,但需要确保不要在fortuneservice上调用任何东西,因为这将导致null ptr异常,除非您对其进行初始化。 On the contrary, if you put @Autowired annotation on top of constructor with argument and it runs fine then that means that the fortuneservice bean that you have declared elsewhere will now be injected inside this class and no null ptr exception if you call some method on that. 相反,如果将@Autowired批注放在带有参数的构造函数的顶部,并且运行良好,则意味着您在其他地方声明的fortuneservice bean现在将被注入该类中,并且如果您在那。

Adding to the previous to answers - think about the term "constructor injection". 在答案的前面加上-考虑术语“构造函数注入”。 You are INJECTING references via constructor parameters. 您正在通过构造函数参数注入引用。 When you @Autowire an empty constructor, there isn't anything to inject. 当@Autowire一个空的构造函数时,没有任何注入。 Therefore you get a NullPointerException when attempting to access the field that was supposed to have a reference injected into. 因此,当您尝试访问应该插入引用的字段时,会收到NullPointerException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM