我正在使用IntelliJ學習spring boot,在運行代碼時遇到了這個問題
2021-09-05 12:55: 48. 665 ERROR 10396 -- [restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
應用程式未能啟動
***************************
描述。
service.EmployeeService中的建構式的引數0需要一個'repository.EmployeeRepository'型別的bean,但無法找到。
行動。
考慮在你的配置中定義一個'repository.EmployeeRepository'型別的bean。
程序結束,退出代碼0。
專案檔案樹看起來像這樣:
├── HELP.md
├──mvnw
├── mvnw.cmd
├── my-first-project.iml
├── pom.xml
├── src
│ ├── Main
│ │ ├── java
│ │ │ ├── com
│ │ │ │ └── Firstproject
│ │ │ │ └── myfirstproject
│ │ │ │ └── MyFirstProjectApplication.java
│ │ │ ├──控制器
│ │ │ │ └── EmployeeController.java
│ │ │ ├──模型
│ │ │ │ └── Employee.java
│ │ │ ├── 儲存庫
│ │ │ │ └── EmployeeRepository.java
│ │ │ └──服務
│ │ │ └── EmployeeService.java
│ │ └──資源
│ │ ├──應用.屬性
│ │ └── static
EmployeeController:
package控制器。
import model.Employee。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController。
import service.EmployeeService。
import java.util.List;
@RestController[/span
@RequestMapping(path = "/api/v1/employee")
public class EmployeeController {
private final EmployeeService employeeService。
@Autowired[/span
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService。
}
@GetMappingpublic List<Employee> getEmployee(){
return employeeService.getEmployee()。
}
}
EmployeeService :
package service。
import model.Employee。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service。
import org.springframework.web.bind.annotation.GetMapping;
import repository.EmployeeRepository;
import java.util.List;
@Service
public class EmployeeService {
final EmployeeRepository employeeRepository;
@Autowired[/span
public EmployeeService(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@GetMapping[/span
public List<Employee> getEmployee(){
return employeeRepository.findAll();
}
}
Employee :
package model;
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Table[/span
public class Employee {
@Id
@SequenceGenerator(name = "emp_sequence",
sequenceName = "emp_sequence",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
生成器 = "emp_sequence")
private Long id;
private String name;
private String email;
private LocalDate dob;
private Float salary;
public Employee() {
} ...Getters and Setters
EmployeeRepository :
package repository。
import model.Employee。
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,long> {
}
MyFirstProjectApplication:
package com.firstproject.myfirstproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"repository", "controller", "service", "model"})
//I added scanBasePackages = {"repository", "controller", "service", "model"} because I had the same problem with other packages and it solved it except for repository
public class MyFirstProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyFirstProjectApplication.class, args)。
}
應用程式的屬性 :
spring.datasource.url=jdbc:postgresql:/localhost:5432/employee
spring.datasource.username=postgres
spring.datasource.password=*********
spring.jpa.hibernate.ddl-auto=creat-drop
spring.jpa.show-sql=true。
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.open-in-view=false。
最后是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 https://maven.apache.org/xsd/maven-4.0.0.xsd"/span>>
<modelVersion>/span>4.0.0</modelVersion>/span>
<parent>/span>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>/span>2.5.4</version>
<relativePath/> <!--從版本庫中查找父本--> < >
</parent>
<groupId>com.firstproject</groupId>
<artifactId>我的第一個專案</artifactId>
<version>0.0.1-SNAPSHOT</version>/span>
<name>我的第一個專案</name>/span>
<description>我的第一個專案</description>
<properties>/span>
<java.version>11</java.version>/span>
</properties>/span>
<dependencies>>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</依賴性>/span>
<dependency>>
<groupId>/span>org.postgresql</groupId>
<artifactId>/span>postgresql</artifactId>
<scope>/span>runtime</scope>/span>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>/span>
</dependencies>
<build>/span>
<plugins>/span>
<plugin>/span>
<groupId>org.springframework.boot</groupId>/span>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>/span>
</plugins>/span>
</build>/span>
</project>
我正在使用:
- java 11
- spring boot(v2.5.4)
- Intellij IDEA 2021.2.1終極版
。
我在使用其他軟體包時也遇到了同樣的問題,但是添加@SpringBootApplication(scanBasePackages = {"controller", "service", "model"})為他們解決了問題。
uj5u.com熱心網友回復:
好吧,Spring的注解掃描不包括包倉庫,所以它不會掃描@Repository來添加它為一個Spring Bean。你還需要定義
@SpringBootApplication(scanBasePackages = {"controller", "service", "model", "repository"})
原因是spring掃描的默認包是com.firstproject.myfirstproject,其中存在MyFirstProjectApplication.claass,它定義了啟動spring boot應用程式的主方法。
考慮到所有其他包都在該包之外,你需要用scanBasePackages手動定義它們,以便spring進入這些其他包,如service、repository和掃描注釋并進行必要的操作。
uj5u.com熱心網友回復:
將controller、repository、model和service這些包移到com.firstproject.myfirstproject。這樣你就可以擺脫scanBasePackages = {"controller", "service", "model"}而只使用@SpringBootApplication,如下:
package com.firstproject.myfirstproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyFirstProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyFirstProjectApplication.class, args)。
}
@SpringBootApplication封裝了@Configuration、@EnableAutoConfiguration和@ComponentScan注釋及其默認屬性。@ComponentScan的默認值意味著@ComponentScan所使用的包上的所有子包都被掃描。這就是為什么在專案的基礎包中包含主類通常是一個很好的做法。
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/320511.html
標籤:
上一篇:IntelliJ簽出的Git分支
