春季启动2.1.4.RELEASE

时间:2019-04-30 03:28:06

标签: java spring spring-boot spring-data-jpa spring-data

我使用Spring Boot 2.1.4.RELEASE创建了一个具有以下依赖项的项目:

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

我有以下实体和存储库:

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "age")
    private Integer age;

    @Column(name = "createdAt")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;
...
}

PersonRepository.java

@Repository
public interface PersonRepository extends CrudRepository<Person, Integer> {

}

以下是我的应用程序类:

@SpringBootApplication
public class SpringDataApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(SpringDataApplication.class, args);
        PersonRepository personRepository = context.getBean(PersonRepository.class);
        Person p1 = new Person("Juan", "Camaney", 55);
        Person p2 = new Person("Arturo", "Lopez", 33);
        Person p3 = new Person("Pancho", "Coscorin", 22);

        personRepository.save(p1);
        personRepository.save(p2);
        personRepository.save(p3);

        Iterator<Person> people = personRepository.findAll().iterator();
        while (people.hasNext()) {
            Person temp = people.next();
            System.out.println(temp);
        }
    }
}

如果我执行我的应用程序,会出现以下错误:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.devs4j.spring.data.repositories.PersonRepository' available

解决方案是添加以下配置类:

@Configuration
@EnableJpaRepositories("com.devs4j.spring.data.repositories")
public class JpaConfiguration {

}

但是我得到了错误:

EnableJpaRepositories cannot be resolved to a type

如果我降级到 2.0.5.RELEASE ,一切正常。

我很困惑,因为当我查看以下春季文档https://docs.spring.io/spring-data/jpa/docs/2.1.6.RELEASE/reference/html/时,我发现它仍在使用@EnableJpaRepositories(“ com.acme.repositories”)

我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

在您的configuration.java中,您是否添加了导入:

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;