我正在为我的网络应用程序使用 SpringBoot,但遇到以下错误:
上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名称为“heroController”的 bean 在文件 [D:\Projects\Java\mydbexxcercise\target\classes\com\db\controllers 中定义时出错\HeroController.class]: bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.db.controllers.HeroController]: Constructor throw exception; 嵌套异常是 java.lang.NullPointerException。
这些是我的课程:
DBApp 类(主类):
package com.db.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/*
* DAL - Data Access Layer
* BL - Business Layer
*
* */
@SpringBootApplication
@ComponentScan(basePackages = {"com.db.controllers","com.db.services"})
@EnableJpaRepositories(basePackages = "com.db.repositories")
@EntityScan(basePackages = "com.db.entities")
public class DBApp
{
public enum PowerCatagory{SpecialPower,Weapon,Machines}
private static ConfigurableApplicationContext appContext;
public static void main(String[] args)
{
appContext = SpringApplication.run(DBApp.class,args);
}
public static ConfigurableApplicationContext getAppContext()
{
return appContext;
}
}
英雄库类:
@Repository
public interface HeroRepository extends JpaRepository<Hero, Integer>
{
}
根据控制台,Spring无法初始化HeroController的原因是以下代码导致的异常:
private HeroService heroService = DBApp.getAppContext().getBean(HeroService.class);
我不知道为什么会在此处抛出异常,因此非常感谢您的帮助。非常感谢您的宝贵时间
慕神8447489
慕容森
相关分类