创建名为" * repository"

时间:2017-01-20 16:16:04

标签: java maven jpa repository spring-data-jpa

我有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 {
...
}

我有这个entiry的存储库。

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:   创建名称为&#39; addressController的bean时出错&#39;:不满意   通过字段“服务”表达的依赖性;嵌套异常是   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为&#39; addressService&#39;:不满意的依赖项的bean时出错   通过字段&#39;存储库表达;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; addressRepository&#39;的bean:调用init方法   失败;嵌套异常是java.lang.AbstractMethodError:   org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg / springframework的/数据/库/型芯/ RepositoryInformation;)Ljava /郎/对象;     在   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(原生方法)   〜[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]引起:   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为&#39; addressService&#39;:不满意的依赖项的bean时出错   通过字段&#39;存储库表达;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; addressRepository&#39;的bean:调用init方法   失败;嵌套异常是java.lang.AbstractMethodError:   org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg / springframework的/数据/库/型芯/ RepositoryInformation;)Ljava /郎/对象;     在   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个常见帧   省略引起:   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; addressRepository&#39;的bean:调用init方法   失败;嵌套异常是java.lang.AbstractMethodError:   org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg / springframework的/数据/库/型芯/ RepositoryInformation;)Ljava /郎/对象;     在   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个常见帧   省略由:java.lang.AbstractMethodError:   org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg / springframework的/数据/库/型芯/ RepositoryInformation;)Ljava /郎/对象;     在   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个常见帧   省略

请帮帮我。 我有简单的应用程序具有相同的结构和逻辑,但它工作正常。所以,我不知道如何修复问题。

3 个答案:

答案 0 :(得分:2)

@NoRepositoryBean添加到您的CrudRepository中。这样一来,仍然可以进行自动配置,而不必手动设置组件扫描基本软件包。

接口org.springframework.data.repository.Repository是实际spring数据存储库的标记。由于您的CrudRepository扩展了Repository,因此spring试图为crudRepository初始化一个名为T的Spring托管存储库(该存储库实际上会转换为Object)。由于Object不是由Hibernate管理的,因此启动时验证失败并崩溃。

@NoRepositoryBean将告诉Spring不要为实现Repository的特定类生成备用存储库。

答案 1 :(得分:0)

我猜你错过了

@Autowired 
Private AddressRepository addressRepository;

在您的服务方式中。

答案 2 :(得分:-2)

问题是没有AddressRepository的实现。

我建议在实际实现而不是接口上使用@Repository和@Service等构造型。

编辑我现在看到你正在使用spring数据来提供AddressRepository接口的实现。也许某些东西没有在那里正确连线?您发布的异常表示无法初始化AddressRepository。以前没有使用过弹簧数据,我不知道为什么会这样。