Java 如何修复:创建名为通过字段表示的未满足依赖项的bean时出错
我正在尝试使用hibernate设置SpringRESTAPI。当试图使用我设置的userRespository时,我遇到了这个错误Java 如何修复:创建名为通过字段表示的未满足依赖项的bean时出错,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我正在尝试使用hibernate设置SpringRESTAPI。当试图使用我设置的userRespository时,我遇到了这个错误 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.potholeapi.models.User
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at com.potholeapi.Application.main(Application.java:20) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:542) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
以下是模型、控制器和应用程序文件
模型
控制器
package com.potholeapi.controllers;
import com.potholeapi.repositories.UserRepository;
import com.potholeapi.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.potholeapi.models.User;
import java.util.ArrayList;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* Get all users list.
*
* @return the list
*/
@GetMapping("/users")
public List<User> getAllUsers() {
return userService.getUsers();
}
}
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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<name>potholeAPI</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
目录结构
src
-main
--java
---com.potholeapi
----controllers
----models
----repositories
----services
-----impl
-resources
pom.xml
我在设置这个时错过了什么?从我所看过的所有教程来看,在线自动连线应该能够正常工作修复软件包结构可能对您有益。如果主应用程序位于名为my.base的包中,而控制器位于my.base.controllers中,则不必使用组件扫描。在当前状态下,我的建议是将repositories包添加到组件扫描中,以便它在运行时找到bean
@ComponentScan({"controllers", "reposistories"})
修复包结构可能对您有益。如果主应用程序位于名为my.base的包中,而控制器位于my.base.controllers中,则不必使用组件扫描。在当前状态下,我的建议是将repositories包添加到组件扫描中,以便它在运行时找到bean
@ComponentScan({"controllers", "reposistories"})
首先,您没有合适的项目打包结构。然后,你没有一个基本的包。Spring总是提到要有一个用于正确组件扫描的基本包。如果没有基本包,这是一种糟糕的做法。 最佳实践应该是:
src
-main
--base package(x.x.x.) (for example : com.project)
---controllers
---models
---repositories
-resources
@ComponentScan(“com.project”)
@ComponentScan({“main.controllers”,“main.repositories”})
应用程序中添加@ComponentScan
类:
@ComponentScan({"main.controllers", "main.repositories"})
@EnableJpaRepositories("main.repositories")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
首先,您没有合适的项目打包结构。然后,你没有一个基本的包。Spring总是提到要有一个用于正确组件扫描的基本包。如果没有基本包,这是一种糟糕的做法。 最佳实践应该是:
src
-main
--base package(x.x.x.) (for example : com.project)
---controllers
---models
---repositories
-resources
@ComponentScan(“com.project”)
@ComponentScan({“main.controllers”,“main.repositories”})
应用程序中添加@ComponentScan
类:
@ComponentScan({"main.controllers", "main.repositories"})
@EnableJpaRepositories("main.repositories")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在
User
类中,您使用int
类型声明id
@Id
@Column(name = "id", unique = true)
private int id;
但是在存储库界面中,您声明了Long
public interface UserRepository extends JpaRepository<User, Long> {}
为了避免出现新错误,请在UserService
界面上使用@Service
注释,如
@Service
public interface UserService {
List<User> getUsers();
}
@服务
公共接口用户服务{
列出getUsers();
}
在User
类中,您使用int
类型声明id
@Id
@Column(name = "id", unique = true)
private int id;
但是在存储库界面中,您声明了Long
public interface UserRepository extends JpaRepository<User, Long> {}
为了避免出现新错误,请在UserService
界面上使用@Service
注释,如
@Service
public interface UserService {
List<User> getUsers();
}
@服务
公共接口用户服务{
列出getUsers();
}
(1)applicationContext.xml
在Spring Boot中已过时。(2) 包名称应使用所有小写字母;与您的导入语句进行比较。(3) @ComponentScan
和类似的配置注释只有在@configuration
类上出现时才会被激活(@springbootplication
包括这些)。(4) 您的配置(应用程序
)需要@EnableJpaRepositories
)。我实施了您建议的更改,但仍然得到相同的结果请更新您的错误消息。我更新了错误消息,我相信它是相同的,请包括您的pom.xml
或build.gradle
文件,并显示您的目录结构(ASCII艺术是常用的方式)。您可能还想尝试@ComponentScan({“controllers”,“repositories”})@EntityScan(“models”)
;我记不得@EnableJpaRepositories
是否会找到一个尚未扫描组件的类型(应该是,但我不确定)。(1)applicationContext.xml
在Spring Boot中已过时。(2) 包名称应使用所有小写字母;与您的导入语句进行比较。(3) @ComponentScan
和类似的配置注释只有在@configuration
类上出现时才会被激活(@springbootplication
包括这些)。(4) 您的配置(应用程序
)需要@EnableJpaRepositories
)。我实施了您建议的更改,但仍然得到相同的结果请更新您的错误消息。我更新了错误消息,我相信它是相同的,请包括您的pom.xml
或build.gradle
文件,并显示您的目录结构(ASCII艺术是常用的方式)。您可能还想尝试@ComponentScan({“controllers”,“repositories”})@EntityScan(“models”)
;我记不得@EnableJpaRepositories
是否会找到一个尚未扫描组件的类型(应该是,但我不确定)。感谢您提供xml、java配置和基于注释的答案。@granadaCoder谢谢。永远为人们而存在:)感谢您提供基于xml、java配置和注释的答案。@granadaCoder谢谢。总是在那里
@Service
public interface UserService {
List<User> getUsers();
}