gpt4 book ai didi

java - org.springframework.beans.BeanInstantiationException : Failed to instantiate

转载 作者:行者123 更新时间:2023-12-03 08:18:09 25 4
gpt4 key购买 nike

我刚刚开始学习Spring Boot。我的代码中有一个错误,内容为

Error creating bean with name 'alien' defined in file[E:\Programing\Java\boot\Project1\target\classes\com\example\demo\Alien.class]:Instantiation of bean failed; nested exception isorg.springframework.beans.BeanInstantiationException: Failed toinstantiate [com.example.demo.Alien]: Constructor threw exception;nested exception is java.lang.NullPointerException: Cannot invoke"com.example.demo.Laptop.compile()" because "this.laptop" is null

下面是我的代码

主类

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();
}

}

外星人等级

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();
}

}

笔记本电脑类

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..");
}

}

最佳答案

问题是您在 Alien 的构造函数中调用 laptop.compile()。当时,Spring 还没有注入(inject) Laptop bean,因此 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......");
}
}

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

@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();
}
}

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

关于java - org.springframework.beans.BeanInstantiationException : Failed to instantiate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68680058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com