org.springframework.beans.BeanInstantiationException:无法实例化

Sha*_*kar 1 java spring spring-boot

我刚刚开始学习 Spring Boot。我的代码中有一个错误,上面写着

在文件 [E:\Programing\Java\boot\Project1\target\classes\com\example\demo\Alien.class] 中定义名称为“alien”的 bean 创建时出错:bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.example.demo.Alien]:构造函数抛出异常;嵌套异常是 java.lang.NullPointerException:无法调用“com.example.demo.Laptop.compile()”,因为“this.laptop”为空

下面是我的代码

主类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Project1Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context= SpringApplication.run(Project1Application.class, args);
        
        Alien a=context.getBean(Alien.class);
        a.show();
    }

}
Run Code Online (Sandbox Code Playgroud)

异类

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Alien {

    private int id;
    private String name;
    private String tech;
    @Autowired
    private Laptop laptop;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTech() {
        return tech;
    }
    public void setTech(String tech) {
        this.tech = tech;
    }
    
    public void show() {
        System.out.println("in show..........");
    }
    public Alien() {
        super();
        System.out.println("Object Created......");
        laptop.compile();
    }
    
}
Run Code Online (Sandbox Code Playgroud)

笔记本类

package com.example.demo;

import org.springframework.stereotype.Component;

@Component
public class Laptop {

    private int lid;
    private String brand;
    
    @Override
    public String toString() {
        return "Laptop [lid=" + lid + ", brand=" + brand + "]";
    }
    public int getLid() {
        return lid;
    }
    public void setLid(int lid) {
        this.lid = lid;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    
    public void compile() {
        System.out.println("Compiling..");
    }
    
}
Run Code Online (Sandbox Code Playgroud)

g00*_*00b 5

问题是您laptop.compile()Alien. 当时,春天没有注入Laptop尚未豆,所以this.laptopnull

有几种方法可以解决这个问题。

首先,您可以将 移动laptop.compile()到一个单独的方法,用@PostConstruct. 例如:

@Component
public class Alien {
    private int id;
    private String name;
    private String tech;
    @Autowired
    private Laptop laptop;
    
    // Getters + Setters
    
    public void show() {
        System.out.println("in show..........");
    }

    // Add this method
    @PostConstruct
    public void initialize() {
        laptop.compile();
    }

    public Alien() {
        super();
        System.out.println("Object Created......");
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以用构造函数注入替换字段注入:

@Component
public class Alien {
    private int id;
    private String name;
    private String tech;
    private Laptop laptop; // Remove @Autowired
    
    // Getters + Setters
    
    public void show() {
        System.out.println("in show..........");
    }


    @Autowired // Put @Autowired here
    public Alien(Laptop laptop) {
        super();
        this.laptop = laptop;
        System.out.println("Object Created......");
        this.laptop.compile();
    }
}
Run Code Online (Sandbox Code Playgroud)

如今,构造函数注入是注入 bean 的首选方式,因此这将是一个可行的解决方案。请注意,即使我@Autowired在构造函数顶部添加了注释,如果这是您唯一的构造函数,则没有必要。


归档时间:

查看次数:

68 次

最近记录:

4 年,3 月 前