我将SpringBoot用于我的Web应用程序,遇到以下错误:
上下文初始化期间遇到的异常-取消刷新尝试:org.springframework.beans.factory.BeanCreationException:在文件[D:\ Projects \ Java \ mydbexxcercise \ target \ classes \ com \中创建名称为'heroController'的bean时出错db \ controllers \ HeroController.class]:实例化bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.db.controllers.HeroController]:构造方法抛出了异常;嵌套的异常是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;
}
}
HeroRepository类:
@Repository
public interface HeroRepository extends JpaRepository<Hero, Integer>
{
}
英雄实体类:
package com.db.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.db.app.DBApp;
import org.postgresql.geometric.PGpolygon;
import org.springframework.stereotype.Component;
@Entity
@Table(name="hero")
public class Hero
{
public int getHid() {
return hid;
}
public PGpolygon getArea() {
return area;
}
public String getSpower() {
return spower;
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public DBApp.PowerCatagory getPc() {
return pc;
}
public float getPower_level() {
return power_level;
}
public float getLuck() {
return luck;
}
@Id private int hid;//This is the table's primary key
public void setHid(int hid) {
this.hid = hid;
}
public void setArea(PGpolygon area) {
this.area = area;
}
public void setSpower(String spower) {
this.spower = spower;
}
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setPc(DBApp.PowerCatagory pc) {
this.pc = pc;
}
public void setPower_level(float power_level) {
this.power_level = power_level;
}
public void setLuck(float luck) {
this.luck = luck;
}
private PGpolygon area;
private String spower;
private String fname;
private String lname;
private DBApp.PowerCatagory pc;
private float power_level;
private float luck;
public Hero(int hid, PGpolygon area, String spower, String fname, String lname, DBApp.PowerCatagory pc, float power_level, float luck) {
this.hid = hid;
this.area = area;
this.spower = spower;
this.fname = fname;
this.lname = lname;
this.pc = pc;
this.power_level = power_level;
this.luck = luck;
}
}
HeroService类:
package com.db.services;
import com.db.entities.Hero;
import com.db.repositories.HeroRepository;
import com.db.app.DBApp;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class HeroService
{
private HeroRepository heroRepository = DBApp.getAppContext().getBean(HeroRepository.class);
public List<Hero> getAllHeroes()
{
List<Hero> res = new ArrayList<>();
heroRepository.findAll().forEach(res::add);
return res;
}
public void addHero(Hero hero)
{
heroRepository.save(hero);
}
public Hero getHero(int id)
{
return heroRepository.findById(id).get();
}
public void updateHero(Hero hero)
{
heroRepository.save(hero);/*If a hero with the same id
already exists in the DB then the save() function
will automatically update that same tuple.*/
}
public void deleteHero(Hero hero)
{
heroRepository.delete(hero);
}
}
HeroController类:
package com.db.controllers;
import com.db.app.DBApp;
import com.db.entities.Hero;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.db.services.HeroService;
import java.util.List;
@RestController
public class HeroController
{
private HeroService heroService = DBApp.getAppContext().getBean(HeroService.class);
@GetMapping("/hero")
public List<Hero> getAllHeroes()
{
//System.out.println(heroService.getAllHeroes());
return heroService.getAllHeroes();
}
@GetMapping("/")
public String test()
{
//System.out.println(heroService.getAllHeroes());
return "working!!!";
}
}
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"
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>
<groupId>MyDBExcercise</groupId>
<artifactId>mydbexxcercise</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>spring-data-aerospike</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.2.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
根据控制台,Spring无法初始化HeroController的原因是以下代码导致的异常:
private HeroService heroService = DBApp.getAppContext().getBean(HeroService.class);
我不知道为什么在这里抛出异常,所以我们将不胜感激。非常感谢您的宝贵时间
答案 0 :(得分:0)
以下2处更改应解决此问题。无需从App Context获取bean。
@Service
public class HeroService {
@Autowired
private HeroRepository heroRepository;
&
@RestController
public class HeroController {
@Autowired
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"
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>MyDBExcercise</groupId>
<artifactId>mydbexxcercise</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>spring-data-aerospike</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
&application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=*****
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
答案 1 :(得分:0)
DBApp
移至com.db
包中。 @SpringBootApplication
并自动检测其他注释getAppContext
方法。 有了这个,您的DBApp
类应该看起来像这样。
package com.db;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DBApp {
public enum PowerCatagory{SpecialPower,Weapon,Machines}
public static void main(String[] args) {
SpringApplication.run(DBApp.class,args);
}
}
现在在您的HeroService
和HeroController
中,您需要使用依赖注入。最佳实践是使用构造函数注入而不是字段注入。
@Service
public class HeroService {
private final HeroRepository heroRepository;
public HeroService(HeroRepository heroRepository) {
this.heroRepository=heroRepository;
}
HeroController
@RestController
public class HeroController {
private final HeroService heroService;
public HeroController(HeroService heroService) {
this.heroService=heroService;
}
注意:@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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<groupId>MyDBExcercise</groupId>
<artifactId>mydbexxcercise</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>spring-data-aerospike</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>