com.**** 中构造函数的参数 0。控制器需要类型为"com.*****"的 bean。服务'。Spring 启动应用程序无法运行



我的代码:

package com.qa.project.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories
@ComponentScan
@SpringBootApplication
@Entity
public class RunStat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
@NotBlank
private double runTime;

@Column
@NotBlank
private double runDistance;

@Column
@NotBlank
private int runCalories;
public RunStat() {
}

public RunStat(Long id, int runCalories, double runDistance, double runTime) {
this.id = id;
this.runCalories = runCalories;
this.runDistance = runDistance;
this.runTime = runTime;

}
public RunStat(int runCalories, double runDistance, double runTime) {
this.runCalories = runCalories;
this.runDistance = runDistance;
this.runTime = runTime;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public double getRunTime() {
return runTime;
}
public void setRunTime(double runTime) {
this.runTime = runTime;
}
public double getRunDistance() {
return runDistance;
}
public void setRunDistance(double runDistance) {
this.runDistance = runDistance;
}
public int getRunCalories() {
return runCalories;
}
public void setRunCalories(int runCalories) {
this.runCalories = runCalories;
}
}
package com.qa.project.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.qa.project.domain.RunStat;
import com.qa.project.repository.RunStatRepo;
@Service
public class RunStatService {
private RunStatRepo repo;
public RunStatService(RunStatRepo repo) {
this.repo = repo;
}

public RunStat addStats(RunStat stats) {
return this.repo.save(stats); 

}
public List<RunStat> getAllStats(){
return this.repo.findAll();
}

public RunStat updateStats(Long id, RunStat newStats) {
Optional<RunStat> optionalRunStat = this.repo.findById(id);
if (optionalRunStat.isPresent()) {
RunStat existingRunStat = optionalRunStat.get();
existingRunStat.setRunTime(newStats.getRunTime());
existingRunStat.setRunDistance(newStats.getRunDistance());
existingRunStat.setRunCalories(newStats.getRunCalories());
return this.repo.save(existingRunStat);
}
return null;

}
public boolean removeStats(Long id) {
this.repo.deleteById(id);
return !this.repo.existsById(id);
}
public RunStat readById(Long id){
Optional<RunStat> optionalRunStat = this.repo.findById(id);
//if (optionalRunStat.isPresent()) { //get rid of this line but use for other methods later
return optionalRunStat.get();
}
}
package com.qa.project.repository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.qa.project.domain.RunStat;
import com.qa.project.service.RunStatService;
@Component
@Repository
public interface RunStatRepo extends JpaRepository<RunStat, Long>{
public RunStat addStats(RunStat stats);
public List<RunStat> getAllStats();
public RunStat updateStats(Long id, RunStat newStats);
public boolean removeStats(Long id);
public Optional<RunStat> findById(Long id);
}
package com.qa.project.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qa.project.domain.RunStat;
import com.qa.project.service.RunStatService;

@RestController
public class RunStatController {

private RunStatService service; 


public RunStatController(RunStatService service) {
super();
this.service = service;
}
@PostMapping("/createStats")
public RunStat addStats(@RequestBody RunStat stats) {       
return this.service.addStats(stats);
}

@GetMapping("/getStats")
public List<RunStat> getAllStats(){
return this.service.getAllStats();
}

@DeleteMapping("/removeStats/{id}") 
public boolean removeStats(@PathVariable Long id) {
return this.service.removeStats(id);    
}

@GetMapping("/getAUser/{id}")
public RunStat readById(@PathVariable Long id){
return this.service.readById(id);
}

@PutMapping("/editUser/{id}") //changes whole record, so you have to input whole new record to replace
public RunStat updateStats(@PathVariable Long id, @RequestBody RunStat stats) {
return this.service.updateStats(id, stats);
}

}
package com.qa.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.context.annotation.Bean;
@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages={"com.qa.service","com.qa.project.domain", "com.qa.project.rest", "com.qa.repository"})
public class QaProjectApplication {
public static void main(String[] args) {
SpringApplication.run(QaProjectApplication.class, args);
}
}
<?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 https://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.6.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.qa</groupId>
<artifactId>QaProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>QaProject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

这里的主要问题是@SpringBootApplication放置错误。Spring Boot应用程序有一个main启动类,它引导整个应用程序。这个类应该得到提到的注释。这是一个特殊的注释,也已经包含了元件扫描。

默认情况下,仅在找到注释的同一个包及其子包中扫描零部件。您已将注释放置到包com.qa.project.domain中的类RunStat上。因此,其他包(如com.qa.project.service(中的所有组件都找不到。

解决方案:

创建一个名为RunStatApplication(或类似名称(的类,并将其放入包com.qa.project中。该类获得注释@SpringBootApplication。有了这个设置,所有其他组件现在都在子包中,因此可以找到。

最新更新