2 回答
TA贡献1780条经验 获得超1个赞
将您的移动
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>
TA贡献1853条经验 获得超18个赞
以下 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
添加回答
举报