【问题标题】:How to use SpringBoot @Autowired in new class constructor如何在新类构造函数中使用 Spring Boot @Autowired
【发布时间】:2022-01-31 14:55:52
【问题描述】:

我创建了一个独立的 SpringBoot 应用程序,并在我的主组件中创建了一个新对象, 但在新课上得到了NullPointerException。 如何正确使用Autowired

package com.aro.try.main;
import com.aro.try.model.AccountInfo;
import com.aro.try.service.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MainProc implements CommandLineRunner {

    @Autowired
    private AccountInfoService accountInfoService;
    @Override
    public void run(String... args) throws Exception {
        System.out.println("Hello MainProc~~~~~~");
//        System.out.printf("account:" + accountInfoService.getAllAccountInfo());
        new ABC().action();
    }
}

class ABC {

    @Autowired
    private AccountInfoService accountInfoService;
    public ABC() {
        System.out.println("Init something~");
    }
    public void action(){
        System.out.println("do action~");
        System.out.println(accountInfoService.getAllAccountInfo());
    }
}

【问题讨论】:

    标签: java spring spring-boot dependency-injection


    【解决方案1】:

    @Autowired 仅适用于 Spring 托管类,而在您的情况下,ABC 不是其中之一。要使 ABC 由 Spring 管理,请使用 @Component 对其进行注释。然后不是在MainProc 中显式实例化ABC,而是使用@Autowired 注入它。 AccountInfoService 可以从 MainProc 中删除,如果它不在那里直接使用。

    @Component
    public class MainProc implements CommandLineRunner {
    
        @Autowired
        private ABC abc;
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Hello MainProc~~~~~~");
            abc.action();
        }
    }
    
    @Component
    class ABC {
    
        @Autowired
        private AccountInfoService accountInfoService;
        public ABC() {
            System.out.println("Init something~");
        }
        public void action() {
            System.out.println("do action~");
            System.out.println(accountInfoService.getAllAccountInfo());
        }
    }
    

    奖励:构造函数注入

    一般建议使用构造函数注入而不是字段注入。其优点在here讨论。

    我们通常建议人们对所有人使用构造函数注入 所有其他属性的强制协作者和设置器注入。 同样,构造函数注入确保所有强制属性都具有 已经满足了,根本不可能实例化一个对象 处于无效状态(未通过其合作者)。

    要使用构造函数注入,请使用 @Autowired 注释构造函数并将依赖项作为参数包含在内。然后,从字段中删除 @Autowired

    @Component
    public class MainProc implements CommandLineRunner {
    
        private ABC abc;
    
        @Autowired
        public MainProc(ABC abc) {
            this.abc = abc;
        }
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Hello MainProc~~~~~~");
            abc.action();
        }
    }
    
    @Component
    public class ABC {
    
        private AccountInfoService accountInfoService;
    
        @Autowired
        public ABC(AccountInfoService accountInfoService) {
            this.accountInfoService = accountInfoService
            System.out.println("Init something~");
        }
    
        public void action() {
            System.out.println("do action~");
            System.out.println(accountInfoService.getAllAccountInfo());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-19
      • 2021-04-04
      • 2021-10-07
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      相关资源
      最近更新 更多