错误org.springframework.beans.factory.BeanCreationException:创建bean Spring Boot时出错

时间:2015-05-12 15:55:16

标签: java hibernate maven spring-boot

我正在做springboot + hibernate应用程序。当我在tomcat服务器上运行应用程序时,我遇到了一些例外。错误:http://pastebin.com/tdwWTvFZ

的pom.xml

    <groupId>pzn</groupId>
    <artifactId>com.mryndak.pzn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.9.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>

        <!-- use UTF-8 for everything -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>


</project>

application.yml

spring:
  datasource:
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/pzn
    username: mateusz
    password: root
  jpa:
    database_platform: org.hibernate.dialect.PostgreSQLDialect

Members.java

package com.mryndak.pzn.entity;

import org.springframework.boot.orm.jpa.EntityScan;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
 * Created by mateusz on 12.05.15.
 */
@Entity
public class Members {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String firstName;
    private String secondName;
    private String lastName;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Member[id=%d, firstName='%s', secondName='%s', lastName='%s'",
                id, firstName, secondName, lastName
        );
    }
}

MembersRepository.java

    package com.mryndak.pzn.repository;

import com.mryndak.pzn.entity.Members;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;


/**
 * Created by mateusz on 12.05.15.
 */
@RepositoryRestResource
public interface MembersRepository extends CrudRepository<Members, Integer> {

    List<Members> findByFirstName(@Param("firstName") String firstName);

}

MembersController.java

package com.mryndak.pzn.controller;

import com.mryndak.pzn.entity.Members;
import com.mryndak.pzn.repository.MembersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mateusz on 12.05.15.
 */

@RestController
public class MambersController {

    @Autowired
    MembersRepository membersRepository;

    @RequestMapping(value = "/api/members/", method = RequestMethod.GET )
    public List<Members> getAll() {
        return (List<Members>) membersRepository.findAll();
    }
}

Application.java

package com.mryndak.pzn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * Created by mateusz on 12.05.15.
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.mryndak.pzn.repository")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

1 个答案:

答案 0 :(得分:1)

删除以下依赖项,它导致版本冲突:

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>

Hibernate 4.3实现了JPA 2.1并带来了自己的API jar版本。 JPA 1.0太旧了。

还删除了hibernate-core的直接依赖关系,因为spring-data-jpa有自己的依赖关系。如果要更改休眠版本,请将以下内容添加到POM:

<properties>
    <hibernate.version>4.3.9.Final</hibernate.version>
</properties>

这将确保Hibernate的所有必需依赖项具有相同的版本(在您的情况下,hibernate-core在4.3.9.Final上,而hibernate-entitymanager仍然在4.3.8.Final上,这导致了冲突)。另请参阅有关该主题的Spring Boot文档:http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#howto-customize-dependency-versions-with-maven