Spring MVC 无法运行springboot,获取依赖注入错误

iecba09b  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(117)

在服务层中,当我注入DAO时,它无法运行服务器。尝试清理缓存和.gradle并清理build。jpa存储库和Kotlinreflect也作为gradle依赖项添加。

错误日志

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-06 01:03:04.625 ERROR 92784 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'businessController' defined in file [/Users/hadi/Documents/house/code/backend-kotlin-rest/api/build/classes/kotlin/main/com/house/api/controller/BusinessController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessServiceImpl': Resolution of declared constructors on bean Class [com.house.api.service.BusinessServiceImpl] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@5598bc99] failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/repository/CrudRepository
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.6.jar:2.6.6]
    at com.house.api.ApiApplicationKt.main(ApiApplication.kt:20) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_322]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_322]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_322]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_322]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.7.1.jar:2.7.1]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessServiceImpl': Resolution of declared constructors on bean Class [com.house.api.service.BusinessServiceImpl] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@5598bc99] failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/repository/CrudRepository
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:309) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1302) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.18.jar:5.3.18]
    ... 23 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/repository/CrudRepository
    at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_322]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:756) ~[na:1.8.0_322]
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_322]
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:473) ~[na:1.8.0_322]
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[na:1.8.0_322]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[na:1.8.0_322]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[na:1.8.0_322]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_322]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362) ~[na:1.8.0_322]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.findClass(RestartClassLoader.java:160) ~[spring-boot-devtools-2.7.1.jar:2.7.1]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:142) ~[spring-boot-devtools-2.7.1.jar:2.7.1]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_322]
    at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.8.0_322]
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) ~[na:1.8.0_322]
    at java.lang.Class.getDeclaredConstructors(Class.java:2020) ~[na:1.8.0_322]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:304) ~[spring-beans-5.3.18.jar:5.3.18]
    ... 36 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.CrudRepository
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387) ~[na:1.8.0_322]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_322]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_322]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_322]
    at java.lang.Class.forName0(Native Method) ~[na:1.8.0_322]
    at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_322]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:145) ~[spring-boot-devtools-2.7.1.jar:2.7.1]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_322]
    ... 52 common frames omitted

Process finished with exit code 0

网域

@Entity
class Business(
    @Id
    @Column(columnDefinition = "uuid")
    override val id: UUID = UUID.randomUUID()
    )

服务

@Service
class BusinessServiceImpl(
    @Autowired private val businessDAO: BusinessDAO // when I inject dependency it fails to run the server. 
) : BusinessService {
    override fun save(addBusinessRequest: AddBusinessRequest): Business? {
        return businessDAO.save(addBusinessRequest.toBusiness())
    }
    fun AddBusinessRequest.toBusiness(): Business {
        return Business(
            name = name)
    }
    fun UpdateBusinessRequest.toBusiness(): Business {
    return Business(
        id = id,
        name = name)
    }
}

刀#

@Repository
interface BusinessDAO :
    CrudRepository<Business, UUID>,
    JpaRepository<Business, UUID>,
    JpaSpecificationExecutor<Business>

日至

data class AddBusinessRequest(
    val name: String? = null)
data class UpdateBusinessRequest(
    val id: UUID,
    val name: String? = null)

应用程序数

@EntityScan // not helping
@EnableJpaRepositories // not helping 
@SpringBootApplication
@RestController // not helping
class ApiApplication
fun main(args: Array<String>) {
    runApplication<ApiApplication>(*args)
}
py49o6xq

py49o6xq1#

首先“BusinessDAO”是一个仓库。在你的代码中,我看到依赖注入过程没有以正确的方式实现。尝试一下,然后告诉我。

#在Maven依赖项中添加此编译器插件

<compilerPlugins>
    <plugin>jpa</plugin>
</compilerPlugins>

#应用程序类

@SpringBootApplication
class ApiApplication

fun main(args: Array<String>) {
    runApplication<ApiApplication>(*args)
}

#域类

@Entity
class Business(
        @Id
        @Column(columnDefinition = "uuid")
        val id: UUID,
        @Column
        val name: String
)

#服务类别

@Service
class BusinessServiceImpl @Autowired constructor(val businessDAO: BusinessDAO) : BusinessService {
    override
    fun save(addBusinessRequest: AddBusinessRequest): Business? {
        return businessDAO.save(addBusinessRequest.toBusiness())
    }

    fun AddBusinessRequest.toBusiness(): Business {
        return Business(UUID.randomUUID(), name = "Name of business")
    }

    fun UpdateBusinessRequest.toBusiness(): Business {
        return Business(
                id = UUID.randomUUID(),
                name = "Updated business")
    }
}

#业务服务类

interface BusinessService {
    open fun save(addBusinessRequest: AddBusinessRequest): Business?
}

或查看链接:https://tedblob.com/kotlin-spring-dependency-injection/

相关问题