org.springframework.beans.factory.UnsatisfiedDependencyException Spring JPA
org.springframework.beans.factory.UnsatisfiedDependencyException Spring JPA
我需要帮助来解决我的大学项目中的这个错误。
我创建了一个在线数据库,并尝试从那里开发一个服务供以后使用,问题是当我 运行 测试该服务时我遇到了这些错误,我查看了很多地方试图纠正它们,但到目前为止所有的尝试都没有成功。
如果有人能检测到正在发生的错误并帮助纠正它,我将不胜感激。
非常感谢。 :D
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
</dependencies>
应用:
package com.teig.DescubraPortugal;
import com.teig.DescubraPortugal.repositories.UtilizadorRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan({"com.teig.DescubraPortugal.controllers","com.teig.DescubraPortugal.services","com.teig.DescubraPortugal.repositories"})
@SpringBootApplication
public class DescubraPortugalApplication {
public static void main(String[] args) {
SpringApplication.run(DescubraPortugalApplication.class, args);
}
}
存储库:
@Repository
public interface UtilizadorRepository extends JpaRepository<Utilizador, Integer> {
}
服务:
@Service
public class UtilizadorService {
@Autowired
UtilizadorRepository utilizadorRepository;
public List<Utilizador> findUsers(){
return utilizadorRepository.findAll();
}
}
控制器:
@RestController
@RequestMapping("user")
public class UtilizadorController {
@Autowired
UtilizadorService utilizasddorService;
@GetMapping("/all")
public List<Utilizador> findUsers(){
return utilizasddorService.findUsers();
}
}
出现错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'utilizadorController': Unsatisfied dependency expressed through field 'utilizasddorService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilizadorService' defined in file [C:\Users\telmo\OneDrive\Documentos\GitHub\DescubraPortugalWS\target\classes\com\teig\DescubraPortugal\services\UtilizadorService.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.teig.DescubraPortugal.services.UtilizadorService] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) com.teig.DescubraPortugal.DescubraPortugalApplication.main(DescubraPortugalApplication.java:23) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilizadorService' defined in file [sun.misc.Launcher$AppClassLoader@18b4aac2]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.teig.DescubraPortugal.services.UtilizadorService] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.3.19.jar:5.3.19]
... 29 common frames omitted ```
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/jpa/repository/JpaRepository
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_291]
at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:738) ~[spring-core-5.3.19.jar:5.3.19]
... 35 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.jpa.repository.JpaRepository
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_291]
再次感谢您的帮助
如错误所述:Spring 无法创建 bean utilizadorController
因为它需要 utilizasddorService
bean 而 Spring 找不到 class .
我打赌这与您的习惯@ComponentScan
有关。
尝试删除它或确保您的包结构如下所示:
com.teig.DescubraPortugal
|- DescubraPortugalApplication.java
|- controllers
| |- UtilizadorController.java
|- services
| |- UtilizadorService.java
|- repositories
| |- UtilizadorRepository.java
不要看你的模型,所以我创建了一个来测试你的应用程序:
package com.teig.descubraportugal.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Utilizador {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
}
该应用程序使用此模型正常工作,但我使用了在内存中工作的 H2 数据库,并带有可通过浏览器访问的控制台。您可以在以下位置进行配置:
- application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
- pom.xml:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
我需要帮助来解决我的大学项目中的这个错误。
我创建了一个在线数据库,并尝试从那里开发一个服务供以后使用,问题是当我 运行 测试该服务时我遇到了这些错误,我查看了很多地方试图纠正它们,但到目前为止所有的尝试都没有成功。 如果有人能检测到正在发生的错误并帮助纠正它,我将不胜感激。
非常感谢。 :D
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
</dependencies>
应用:
package com.teig.DescubraPortugal;
import com.teig.DescubraPortugal.repositories.UtilizadorRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan({"com.teig.DescubraPortugal.controllers","com.teig.DescubraPortugal.services","com.teig.DescubraPortugal.repositories"})
@SpringBootApplication
public class DescubraPortugalApplication {
public static void main(String[] args) {
SpringApplication.run(DescubraPortugalApplication.class, args);
}
}
存储库:
@Repository
public interface UtilizadorRepository extends JpaRepository<Utilizador, Integer> {
}
服务:
@Service
public class UtilizadorService {
@Autowired
UtilizadorRepository utilizadorRepository;
public List<Utilizador> findUsers(){
return utilizadorRepository.findAll();
}
}
控制器:
@RestController
@RequestMapping("user")
public class UtilizadorController {
@Autowired
UtilizadorService utilizasddorService;
@GetMapping("/all")
public List<Utilizador> findUsers(){
return utilizasddorService.findUsers();
}
}
出现错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'utilizadorController': Unsatisfied dependency expressed through field 'utilizasddorService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilizadorService' defined in file [C:\Users\telmo\OneDrive\Documentos\GitHub\DescubraPortugalWS\target\classes\com\teig\DescubraPortugal\services\UtilizadorService.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.teig.DescubraPortugal.services.UtilizadorService] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) com.teig.DescubraPortugal.DescubraPortugalApplication.main(DescubraPortugalApplication.java:23) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilizadorService' defined in file [sun.misc.Launcher$AppClassLoader@18b4aac2]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.teig.DescubraPortugal.services.UtilizadorService] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.3.19.jar:5.3.19]
... 29 common frames omitted ```
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/jpa/repository/JpaRepository
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_291]
at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:738) ~[spring-core-5.3.19.jar:5.3.19]
... 35 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.jpa.repository.JpaRepository
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_291]
再次感谢您的帮助
如错误所述:Spring 无法创建 bean utilizadorController
因为它需要 utilizasddorService
bean 而 Spring 找不到 class .
我打赌这与您的习惯@ComponentScan
有关。
尝试删除它或确保您的包结构如下所示:
com.teig.DescubraPortugal
|- DescubraPortugalApplication.java
|- controllers
| |- UtilizadorController.java
|- services
| |- UtilizadorService.java
|- repositories
| |- UtilizadorRepository.java
不要看你的模型,所以我创建了一个来测试你的应用程序:
package com.teig.descubraportugal.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Utilizador {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
}
该应用程序使用此模型正常工作,但我使用了在内存中工作的 H2 数据库,并带有可通过浏览器访问的控制台。您可以在以下位置进行配置:
- application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
- pom.xml:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>