简体   繁体   English

为使用 Java 配置创建的自动装配 bean 获取 null

[英]Getting null for Autowired bean created using Java config

I am creating a REST service in Spring boot.我正在 Spring 引导中创建 REST 服务。 I am creating a bean in config class and trying to use in service class by auto wiring, but I am always getting null, I have tried in constructor injection as well but not working.我在配置 class 中创建一个 bean,并尝试通过自动接线在服务 class 中使用,但我总是得到 null,我也尝试过构造函数注入但不工作。 Below is the code,下面是代码,

Main app主应用

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

REST controller REST controller

@RestController
@RequestMapping("/v1")
public class RestController {

    @Autowired
    private Service service;

Service服务

@Service
public class ServiceImpl implements Service {
    //This is the bean
    @Autowired
    private Record record;
    
    public ServiceImpl() {   //-----------------> tried injecting in constructor as well
        System.out.println(record); //-------------------> null
    }

Config class配置 class

@Configuration
public class AppConfig {
    
    @Bean
    public Record record() {
        return new Record("test");
    }
}

I noted whenever I remove the record() from config class I get below error我注意到每当我从配置 class 中删除记录()时,我都会遇到以下错误

required a bean of type 'com.ns.service.Record' that could not be found

And after adding the method the error is not reported but null is returned, which indirectly means record() is considered as returning the required bean.并且添加方法后没有报错但是返回了null,间接意味着record()被认为是返回了需要的bean。 I can't find what I am doing wrong please advise.我找不到我做错了什么,请指教。

Project folder structure项目文件夹结构项目文件夹结构

I think you're doing everything right conceptually我认为你在概念上做的一切都是正确的

Spring creates an object first and only after that injects the values (technically done in bean post processors): Spring 首先创建一个 object,然后才注入值(技术上在 bean 后处理器中完成):

So try this:所以试试这个:

@Service
public class ServiceImpl implements Service {
    //This is the bean
    @Autowired
    private Record record;
    
    public ServiceImpl() {   
        // here the record is null - not injected yet
        System.out.println(record); 
    }

    @PostConstruct
    public void checkThisOut() {
      // here print the record
    }

You say you've tried constructor injection as well - it should work because spring has to inject something into the constructor of a bean (ServiceImpl) or fail.您说您也尝试过构造函数注入 - 它应该可以工作,因为 spring 必须向 bean (ServiceImpl) 的构造函数中注入一些东西,否则会失败。 Please show the code snippet请显示代码片段

One this that might be wrong in some level (although it doesn't sound like this from your description) is that you have to put all the @Configuration / @Service annotated classes in the package that is the same or underneath the package where you've created the main class annotated with @SpringBootApplication annotation.这在某种程度上可能是错误的(尽管从您的描述中听起来不像这样)是您必须将所有@Configuration / @Service注释类放在 package 中,该类与您所在的 package 相同或下方'已经创建了带有@SpringBootApplication注释的主要 class 注释。 It instructs spring boot where to look for the beans.它指示 spring 引导到哪里寻找 bean。

So make sure your classes obey this rule...因此,请确保您的班级遵守此规则...

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

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