vis*_*sst 62 spring repository spring-data spring-data-jpa spring-boot
我正在开发一个spring boot应用程序,我在这里遇到了一个问题.我正在尝试注入一个@Repository注释接口,它似乎根本不起作用.我收到了这个错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootRunner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.pharmacy.config.SpringBootRunner.main(SpringBootRunner.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 18 common frames omitted
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
主要应用类:
package com.pharmacy.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("org.pharmacy")
public class SpringBootRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootRunner.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
实体类:
package com.pharmacy.persistence.users;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class UserEntity {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
}
Run Code Online (Sandbox Code Playgroud)
存储库界面:
package com.pharmacy.persistence.users.dao;
import com.pharmacy.persistence.users.UserEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserEntityDao extends CrudRepository<UserEntity,Long>{
}
Run Code Online (Sandbox Code Playgroud)
控制器:
package com.pharmacy.controllers;
import com.pharmacy.persistence.users.UserEntity;
import com.pharmacy.persistence.users.dao.UserEntityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@Autowired
UserEntityDao userEntityDao;
@RequestMapping(value = "/")
public String hello() {
userEntityDao.save(new UserEntity("ac"));
return "Test";
}
}
Run Code Online (Sandbox Code Playgroud)
的build.gradle
buildscript {
ext {
springBootVersion = '1.2.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
mainClassName = "com.pharmacy.config.SpringBootRunner"
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-ws")
compile("postgresql:postgresql:9.0-801.jdbc4")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Run Code Online (Sandbox Code Playgroud)
application.properties:
spring.view.prefix: /
spring.view.suffix: .html
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=abc123
Run Code Online (Sandbox Code Playgroud)
我甚至将我的代码与访问数据jpa进行了比较,并且我已经没有想法这个代码有什么问题了.任何帮助赞赏.提前致谢.
编辑:我改变了我的代码,如上所示,当我将我的@Repository接口注入另一个组件时,我没有收到该错误.但是,我现在遇到了一个问题 - 我的组件无法检索(我使用过调试).我做错了所以春天找不到我的组件?
han*_*321 134
当存储库包与@SpringBootApplication/不同时@EnableAutoConfiguration,@EnableJpaRepositories需要明确定义基本包.
尝试添加@EnableJpaRepositories("com.pharmacy.persistence.users.dao")到SpringBootRunner
小智 27
我没有找到存储库时遇到同样的问题.所以我所做的就是将所有内容都移到一个包中.这意味着我的代码没有任何问题.我将Repos和Entities移动到另一个包中,并将以下内容添加到SpringApplication类中.
@EnableJpaRepositories("com...jpa")
@EntityScan("com...jpa")
Run Code Online (Sandbox Code Playgroud)
之后,我将Service(接口和实现)移动到另一个包,并将以下内容添加到SpringApplication类中.
@ComponentScan("com...service")
Run Code Online (Sandbox Code Playgroud)
这解决了我的问题.
Dhe*_*rik 17
我想分享这类问题还有另外一个原因,因为我在这个问题上挣扎了一段时间,我找不到任何答案.
在像以下的存储库中:
@Repository
public interface UserEntityDao extends CrudRepository<UserEntity, Long>{
}
Run Code Online (Sandbox Code Playgroud)
如果实体UserEntity 不具有的@Entity对类注释,你会有同样的错误.
这种错误令人困惑,因为您专注于尝试解决有关Spring未找到存储库但问题是实体的问题.如果您在尝试测试存储库时得到了这个答案,这个答案可能会对您有所帮助.
Dam*_*ato 13
看来您的@ComponentScan注释设置不正确.试试:
@ComponentScan(basePackages = {"com.pharmacy"})
Run Code Online (Sandbox Code Playgroud)
实际上,如果您的主类位于结构的顶部,例如直接在com.pharmacy包下,则不需要组件扫描.
此外,您不需要两者
@SpringBootApplication
@EnableAutoConfiguration
Run Code Online (Sandbox Code Playgroud)
该@SpringBootApplication注释包括@EnableAutoConfiguration默认.
小智 12
我有一个类似的问题,我NoSuchBeanDefinitionException在Spring Boot 中接收(基本上在处理CRUD存储库时),我不得不在主类上添加以下注释:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages={"<base package name>"})
@EnableJpaRepositories(basePackages="<repository package name>")
@EnableTransactionManagement
@EntityScan(basePackages="<entity package name>")
Run Code Online (Sandbox Code Playgroud)
此外,请确保@Component在实现上具有注释.
小智 7
在 SpringBoot 中,默认情况下不会自动启用 JpaRepository。您必须明确添加
@EnableJpaRepositories("packages")
@EntityScan("packages")
Run Code Online (Sandbox Code Playgroud)
小智 6
@SpringBootApplication(scanBasePackages=,<youur package name>)
@EnableJpaRepositories(<you jpa repo package>)
@EntityScan(<your entity package>)
Entity class like below
@Entity
@Table(name="USER")
public class User {
@Id
@GeneratedValue
Run Code Online (Sandbox Code Playgroud)
小智 5
为了扩展上述答案,您实际上可以在 EnableJPARepositories 标记中添加多个包,这样您在仅指定存储库包后就不会遇到“对象未映射”错误。
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.test.model", "com.test.repository"})
public class SpringBootApplication{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
115654 次 |
| 最近记录: |