java Spring io @Autowired:空白的最终字段可能尚未初始化

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

Spring io @Autowired: The blank final field may not have been initialized

javaspringspring-mvcspring-data

提问by Pinwheeler

what I assume is a pretty basic question here-

我认为这是一个非常基本的问题 -

There are several flavors of questions regarding this error, but none in the first 5 results that have the added nuance of Spring.

关于此错误有多种问题,但在前 5 个结果中没有一个具有 Spring 的细微差别。

I have the beginnings of a REST-ful webapp written in spring. I am trying to connect it to a database.

我有一个在 spring 中编写的 REST-ful webapp 的开始。我正在尝试将其连接到数据库。

I have an entity named Workspace and am trying to use the spring injection of a bean( correct terminology ?) to save an instance of the workspace entity

我有一个名为 Workspace 的实体,我正在尝试使用 bean 的 spring 注入(正确的术语?)来保存工作区实体的实例

package com.parrit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.parrit.models.Workspace;
import com.parrit.models.WorkspaceRepository;

@RestController
@RequestMapping("/workspace")
public class WorkspaceController {

    @Autowired
    private final WorkspaceRepository repository;

    @RequestMapping(method = RequestMethod.POST)
    void save( @RequestBody String workspaceHTML) {
        Workspace ws = new Workspace();
        ws.setHTML(workspaceHTML);
        repository.save(ws);
    }
}

My error is on the repository variable private final WorkspaceRepository repository. The compiler is complaining that it may not be initialized and attempting to run the app yields the same result.

我的错误出在存储库变量上private final WorkspaceRepository repository。编译器抱怨它可能未初始化并尝试运行该应用程序会产生相同的结果。

How do I get an instance of this repository object into my controller in order to do save operations on it?

如何将这个存储库对象的实例放入我的控制器中以便对其进行保存操作?

回答by JB Nizet

Having @Autowiredand final on a field are contradictory.

@Autowired一个领域上的拥有和最终是矛盾的。

The latter says: this variable has one and only one value, and it's initialized at construction time.

后者说:这个变量只有一个值,并且在构造时初始化。

The former says: Spring will construct the object, leaving this field as null (its default value). Then Spring will use reflection to initialize this field with a bean of type WorkspaceRepository.

前者说:Spring 将构造对象,将此字段保留为 null(其默认值)。然后 Spring 将使用反射来用 WorkspaceRepository 类型的 bean 初始化这个字段。

If you want final fields autowired, use constructor injection, just like you would do if you did the injection by yourself:

如果您想自动装配 final 字段,请使用构造函数注入,就像您自己进行注入一样:

@Autowired
public WorkspaceController(WorkspaceRepository repository) {
    this.repository = repository;
}

回答by Sleiman Jneidi

Exactly, you have to provide a constructor that assigns the final field

确切地说,您必须提供一个分配最终字段的构造函数

private final WorkspaceRepository repository;

@Autowired
public WorkspaceController(WorkspaceRepository repository){
  this.repository = repository;
}

And Spring will be able to figure out how to initialise the object and inject the repository via the constructor

Spring 将能够弄清楚如何初始化对象并通过构造函数注入存储库