未能实例化类 HeroController;构造函数抛出异常

我正在为我的网络应用程序使用 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);

我不知道为什么会在此处抛出异常,因此非常感谢您的帮助。非常感谢您的宝贵时间


翻过高山走不出你
浏览 99回答 2
2回答

慕神8447489

将您的移动DBApp到com.db包。删除所有注释,但@SpringBootApplication其他注释是隐含的和自动检测的删除getAppContext方法。这样你的DBApp班级应该看起来像这样。package com.db;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DBApp {&nbsp; &nbsp; public enum PowerCatagory{SpecialPower,Weapon,Machines}&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(DBApp.class,args);&nbsp; &nbsp; }}现在在你HeroService和HeroController你需要使用依赖注入。最佳实践是使用构造函数注入而不是字段注入。@Servicepublic class HeroService {&nbsp; private final HeroRepository heroRepository;&nbsp; public HeroService(HeroRepository heroRepository) {&nbsp; &nbsp; &nbsp;this.heroRepository=heroRepository;&nbsp; }这HeroController@RestControllerpublic class HeroController {&nbsp; private final HeroService heroService;&nbsp; public HeroController(HeroService heroService) {&nbsp; &nbsp; this.heroService=heroService;&nbsp; }注意:上的@Repository注释HeroRepository可以删除,因为它没有添加任何内容。接下来你的依赖有点乱,使用专用的 spring-boot-starters 来获得正确的和经过测试的版本。您不需要 Hibernate/JPA 依赖项(它们包含在 中spring-boot-starter-data-jpa),其他人需要较新的版本。<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>MyDBExcercise</groupId>&nbsp; &nbsp; <artifactId>mydbexxcercise</artifactId>&nbsp; &nbsp; <version>1.0-SNAPSHOT</version>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.4.RELEASE</version>&nbsp; &nbsp; </parent>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.aerospike</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-data-aerospike</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.0.1.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-jpa</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.postgresql</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>postgresql</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build></project>

慕容森

以下 2 个更改应该可以解决此问题。无需从 App Context 获取 bean。@Servicepublic class HeroService {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private HeroRepository heroRepository;&@RestControllerpublic class HeroController {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private HeroService heroService;& pom.xml<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent>&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; <version>2.1.4.RELEASE</version>&nbsp; &nbsp; <relativePath/> <!-- lookup parent from repository --></parent><groupId>MyDBExcercise</groupId><artifactId>mydbexxcercise</artifactId><version>0.0.1-SNAPSHOT</version><properties>&nbsp; &nbsp; <java.version>1.8</java.version></properties><dependencies>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter</artifactId>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.aerospike</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-data-aerospike</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>1.0.2.RELEASE</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-jpa</artifactId>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.postgresql</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>postgresql</artifactId>&nbsp; &nbsp; </dependency></dependencies><build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build>& application.propertiesspring.datasource.url=jdbc:postgresql://localhost:5432/postgresspring.datasource.username=postgresspring.datasource.password=*****spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java