我正在开发一个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
这是我的代码:
主要应用类:
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);
}
}
实体类:
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;
}
存储库界面:
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>{
}
控制器:
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";
}
}
的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")
}
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
我甚至将我的代码与Accessing data jpa进行了比较,而且我对这段代码的错误一无所知。 任何帮助赞赏。提前谢谢。
编辑:我改变了我的代码,如上所示,当我将我的@Repository接口注入另一个组件时,我没有收到该错误。但是,我现在遇到了一个问题 - 我的组件无法检索(我使用过调试)。我做错了所以春天找不到我的组件?
答案 0 :(得分:112)
当存储库包与@SpringBootApplication
/ @EnableAutoConfiguration
不同时,需要明确定义@EnableJpaRepositories
的基本包。
尝试将@EnableJpaRepositories("com.pharmacy.persistence.users.dao")
添加到SpringBootRunner
答案 1 :(得分:20)
我遇到了与Repository无法找到相同的问题。所以我所做的就是将所有内容都移到一个包中。这意味着我的代码没有任何问题。我移动了Repos&amp;实体进入另一个包并将以下内容添加到SpringApplication类中。
@EnableJpaRepositories("com...jpa")
@EntityScan("com...jpa")
之后,我将服务(接口和实现)移动到另一个包,并将以下内容添加到SpringApplication类中。
@ComponentScan("com...service")
这解决了我的问题。
答案 2 :(得分:12)
您的@ComponentScan
注释似乎未正确设置。
试试:
@ComponentScan(basePackages = {"com.pharmacy"})
实际上,如果您的主类位于结构的顶部,则不需要进行组件扫描,例如直接在com.pharmacy
包下。
另外,你不需要两者
@SpringBootApplication
@EnableAutoConfiguration
@SpringBootApplication
注释默认包含@EnableAutoConfiguration
。
答案 3 :(得分:10)
我想分享这类问题还有另一个原因,因为我在这个问题上挣扎了一段时间,而且我找不到任何答案。
在像以下的存储库中
@Repository
public interface UserEntityDao extends CrudRepository<UserEntity, Long>{
}
如果您的实体UserEntity
没有该类的@Entity
注释,您将遇到相同的错误。
此错误令人困惑,因为您专注于尝试解决有关Spring未找到存储库但问题是实体的问题。如果您在尝试测试存储库时得到了这个答案,this answer可能对您有帮助。
答案 4 :(得分:9)
我有一个类似的问题,我在Spring Boot中接收NoSuchBeanDefinitionException
(基本上在处理CRUD存储库时),我不得不在主类上添加以下注释:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages={"<base package name>"})
@EnableJpaRepositories(basePackages="<repository package name>")
@EnableTransactionManagement
@EntityScan(basePackages="<entity package name>")
另外,请确保在实施中具有@Component
注释。
答案 5 :(得分:3)
要扩展到上述答案,您实际上可以在EnableJPARepositories标记中添加多个软件包,以便仅指定存储库软件包后就不会遇到“对象未映射”错误。
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.test.model", "com.test.repository"})
public class SpringBootApplication{
}
答案 6 :(得分:3)
@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
答案 7 :(得分:2)
这是个错误:就像之前说过的那样,您在组件扫描中使用的是由com.pharmacy插入的org.pharmacy
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 {
答案 8 :(得分:2)
在SpringBoot中,默认情况下不会自动启用JpaRepository。您必须明确添加
@EnableJpaRepositories("packages")
@EntityScan("packages")
答案 9 :(得分:1)
您正在扫描错误的软件包: @ComponentScan(“ org .pharmacy”)
应位于的位置: @ComponentScan(“ com .pharmacy”)
因为您的软件包名称以com而不是org开头。
答案 10 :(得分:1)
我对这个话题也有一些问题。 您必须确保在Spring boot runner类中定义包,如下例所示:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"controller", "service"})
@EntityScan("entity")
@EnableJpaRepositories("repository")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
我希望这有帮助!
答案 11 :(得分:1)
可能与您拥有的包装有关。我有一个类似的问题:
Description:
Field userRepo in com.App.AppApplication required a bean of type 'repository.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
操作:
考虑在您的配置中定义类型为“ repository.UserRepository
”的bean。
“
通过将存储库文件放入具有标准化命名约定的软件包中来解决此问题:
e.g. com.app.Todo (for main domain files)
和
com.app.Todo.repository (for repository files)
那样,spring知道去哪里寻找存储库,否则事情会很快变得混乱。 :)
希望这会有所帮助。
答案 12 :(得分:1)
我有类似的问题,但原因不同:
就我而言,问题在于定义存储库的界面
public interface ItemRepository extends Repository {..}
我省略了模板的类型。设置正确:
public interface ItemRepository extends Repository<Item,Long> {..}
做了这个伎俩。
答案 13 :(得分:0)
在@ComponentScan("org.pharmacy")
中,您声明 org.pharmacy
软件包。
但是您的组件位于 com.pharmacy
包中。
答案 14 :(得分:0)
如果在使用@DataJpaTest
进行单元测试时遇到此问题,那么您将在下面找到解决方案。
Spring Boot不会为@Repository
初始化@DataJpaTest
bean。因此,请尝试以下两种修复方法之一以使它们可用:
第一
改为使用@SpringBootTest
。但这将启动整个应用程序上下文。
第二(更好的解决方案)
导入所需的特定存储库,如下所示
@DataJpaTest
@Import(MyRepository.class)
public class MyRepositoryTest {
@Autowired
private MyRepository myRepository;
答案 15 :(得分:0)
Spring Data MongoDB遇到类似的问题:我必须将包路径添加到@EnableMongoRepositories
答案 16 :(得分:0)
确保试图自动链接存储库的@Service
或@Component
与SpringApplication.class
不在同一目录中。确保它位于service/
之类的子文件夹中。
答案 17 :(得分:0)
有时候我忘记将Lombok批注处理器依赖项添加到Maven配置中时遇到相同的问题