【发布时间】:2022-08-11 03:28:51
【问题描述】:
我不确定为什么会收到此错误,但我怀疑它来自 TestApplication.java。我尝试了各种方法,例如Question1,但无法弄清楚出了什么问题。
我引用了这个video 来构建这个应用程序。
错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.User.UserService required a bean
of type \'com.example.User.UserRepository\' that could not be found.
Action:
Consider defining a bean of type \'com.example.User.UserRepository\' in your
configuration.
测试应用程序.java
package com.example.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.example.User.UserController;
@SpringBootApplication
@ComponentScan(basePackageClasses = {UserController.class})
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
用户.java
package com.example.User;
import java.time.LocalDate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = \"USER\")
public class User {
@Id
@SequenceGenerator(
name = \"user_sequence\",
sequenceName = \"user_sequence\",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = \"user_sequence\"
)
private Long id;
private String PDGA_id;
private String first_name;
private String last_name;
private String email;
private boolean email_verified;
private String phone_number;
private String password;
private LocalDate created_date;
private LocalDate modified_date;
public User(){
}
public User(Long id,
String PDGA_id,
String first_name,
String last_name,
String email,
boolean email_verified,
String phone_number,
String password,
LocalDate created_date,
LocalDate modified_date)
{
this.id = id;
this.PDGA_id = PDGA_id;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.email_verified = email_verified;
this.phone_number = phone_number;
this.password = password;
this.created_date = created_date;
this.modified_date = modified_date;
}
public User(String PDGA_id,
String first_name,
String last_name,
String email,
boolean email_verified,
String phone_number,
String password,
LocalDate created_date,
LocalDate modified_date)
{
this.PDGA_id = PDGA_id;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.email_verified = email_verified;
this.phone_number = phone_number;
this.password = password;
this.created_date = created_date;
this.modified_date = modified_date;
}
// Getter-Setter
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getPDGA_id() {
return this.PDGA_id;
}
public void setPDGA_id(String PDGA_id) {
this.PDGA_id = PDGA_id;
}
public String getFirst_name() {
return this.first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return this.last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isEmail_verified() {
return this.email_verified;
}
public void setEmail_verified(boolean email_verified) {
this.email_verified = email_verified;
}
public String getPhone_number() {
return this.phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public LocalDate getCreated_date() {
return this.created_date;
}
public void setCreated_date(LocalDate created_date) {
this.created_date = created_date;
}
public LocalDate getModified_date() {
return this.modified_date;
}
public void setModified_date(LocalDate modified_date) {
this.modified_date = modified_date;
}
@Override
public String toString(){
return \"User{\" +
\"id =\" + id +
\", PDGA_id\" + PDGA_id + \'\\\'\' +
\", first_name\" + first_name + \'\\\'\' +
\", last_name\" + last_name + \'\\\'\' +
\", email\" + email + \'\\\'\' +
\", email_verified\" + email_verified +
\", phone_number\" + phone_number + \'\\\'\' +
\", password\" + password + \'\\\'\' +
\", created_date\" + created_date +
\", modified_date\" + modified_date +
\"}\";
}
}
用户控制器.java
package com.example.User;
import java.util.List;
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;
@RestController
@RequestMapping(path = \"api/v1/user\")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService){
this.userService = userService;
}
@GetMapping()
public List<User> getUsers(){
return userService.getUsers();
}
}
用户存储库.java
package com.example.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
用户服务.java
package com.example.User;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository){
this.userRepository = userRepository;
}
public List<User> getUsers(){
return userRepository.findAll();
}
}
应用程序属性
spring.datasource.url=jdbc:mariadb://localhost:3306/LiensDiGoLinks
spring.datasource.username=root
spring.datasource.password=Password123!
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.format_sql=true
-
嗨,从 TestApplication.class 中删除此注释 @ComponentScan(basePackageClasses = {UserController.class})。由于这个 spring 无法扫描和创建 bean。
标签: java spring spring-boot hibernate spring-data-jpa