创建名称为“*repository”的 bean 时出错
Posted
技术标签:
【中文标题】创建名称为“*repository”的 bean 时出错【英文标题】:Error creating bean with name "*repository" 【发布时间】:2017-06-05 16:46:02 【问题描述】:我有一个 Maven 项目。 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>root</groupId>
<artifactId>CourseWork5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>coursework</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
另外,我有一个实体地址。
package root.entity;
import javax.persistence.*;
@Entity
@Table(name = "Address")
public class Address
...
我有这个条目的存储库。
package root.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import root.entity.Address;
@Repository
public interface AddressRepository extends JpaRepository<Address, Integer>
...
以及带有实现的服务接口。
package root.service.interfaces;
import root.entity.Address;
public interface AddressServiceInterface
...
package root.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import root.entity.Address;
import root.repository.AddressRepository;
import root.service.interfaces.AddressServiceInterface;
@Service
public class AddressService implements AddressServiceInterface
...
我的 application.properties 文件:
#DB properties:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/labsdb?useSSL=false
db.username=root
db.password=rootPassword
#Hibernate Configuration:
db.hibernate.dialect=org.hibernate.dialect.MySQLDialect
db.hibernate.show_sql=true
db.entitymanager.packages.to.scan=root.entity
db.hibernate.hbm2ddl.auto = create
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/labsdb
spring.datasource.username=root
spring.datasource.password=rootPassword
security.require-ssl=false
我的开始文件:
package root;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApp
public static void main(String[] args)
SpringApplication.run(TestApp.class, args);
但是,运行应用程序后出现错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'addressController': Unsatisfied
dependency expressed through field 'service'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'addressService': Unsatisfied dependency
expressed through field 'repository'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'addressRepository': Invocation of init method
failed; nested exception is java.lang.AbstractMethodError:
org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:592)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at
root.TestApp.main(TestApp.java:10) [classes/:na] at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
~[na:1.8.0_65] at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:1.8.0_65] at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
~[na:1.8.0_65] at java.lang.reflect.Method.invoke(Method.java:497)
~[na:1.8.0_65] at
org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
[spring-boot-devtools-1.4.0.RELEASE.jar:1.4.0.RELEASE] Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'addressService': Unsatisfied dependency
expressed through field 'repository'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'addressRepository': Invocation of init method
failed; nested exception is java.lang.AbstractMethodError:
org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:592)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1131)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] ... 24 common frames
omitted Caused by:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'addressRepository': Invocation of init method
failed; nested exception is java.lang.AbstractMethodError:
org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1131)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] ... 37 common frames
omitted Caused by: java.lang.AbstractMethodError:
org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
at
org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199)
~[spring-data-commons-1.12.2.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266)
~[spring-data-commons-1.12.2.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252)
~[spring-data-commons-1.12.2.RELEASE.jar:na] at
org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:84)
~[spring-data-jpa-1.3.3.jar:na] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] ... 47 common frames
omitted
请帮帮我。 我有一个具有相同结构和逻辑的简单应用程序,但它运行良好。所以,我不知道如何解决这个问题。
【问题讨论】:
您能找到解决方案吗? 【参考方案1】:将@NoRepositoryBean
添加到您的CrudRepository
。这将允许自动配置仍然发生,而无需手动设置组件扫描基础包。
接口org.springframework.data.repository.Repository
是实际spring 数据存储库的标记。由于您的CrudRepository
扩展了Repository
,spring 正在尝试为T
初始化一个名为crudRepository
的Spring 托管存储库(它基本上转换为对象)。由于 Object 不是由 Hibernate 管理的,因此它在启动时验证失败并崩溃。
@NoRepositoryBean
将告诉 Spring 不要为实现 Repository
的特定类生成支持存储库。
【讨论】:
为了将来,将代码包装在反引号 `` 中,以便将其格式化为代码。它使问题和答案更清晰,更易于阅读。 谢谢你,你拯救了我的一天!【参考方案2】:我猜你错过了
@Autowired
Private AddressRepository addressRepository;
在您的服务方法中。
【讨论】:
【参考方案3】:问题是没有实现AddressRepository。
我建议在实际实现而不是接口上使用诸如@Repository 和@Service 之类的构造型。
edit 我现在看到您正在使用 spring 数据来提供 AddressRepository 接口的实现。也许那里没有正确连接?您发布的异常表明无法初始化 AddressRepository。之前没用过spring数据,不知道为什么会这样。
【讨论】:
界面添加@Repository!以上是关于创建名称为“*repository”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章
没有可用的“repository.Dao”类型的合格 bean
为啥我们得到 BeanCreationException:创建名称为“springSecurityFilterChain”的 bean 时出错? [复制]
在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误
如何修复 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“name”的 bean 时出错?
WebLogic 10.3.7 文件上传返回“创建名称为 'portletMultipartResolver' 的 bean 时出错”