Bean方法' dataSource'没装

时间:2017-03-14 21:27:35

标签: hibernate jpa spring-boot

当我收到这些错误消息时,我正在使用其他版本(1.5.2.RELEASE)的春季启动版本1.3.5教程:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

这是我的课程:

package br.com.myspringproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringProjectApplication.class, args);
    }
}
package br.com.myspringproject;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MySpringProjectApplication.class);
    }

}
package br.com.myspringproject.domain;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Client {

    @Id
    @GeneratedValue
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
package br.com.myspringproject.service;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.myspringproject.domain.Client;

@Service
public class ClientService {

    @Autowired
    ClientRepository clientRepository;

    public Client saveBLA(Client client){

        return clientRepository.save(client);       
    }

    public Collection<Client> findAllBLA(){
        return clientRepository.findAll();
    }

    public Client findOneBLA(Integer id){
        return clientRepository.findOne(id);
    }

    public void deleteBLA(Client client){
        clientRepository.delete(client);
    }
}
package br.com.myspringproject.service;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import br.com.myspringproject.domain.Client;

@Repository
public interface ClientRepository extends JpaRepository<Client,Integer>{

}
package br.com.myspringproject.web;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import br.com.myspringproject.domain.Client;
import br.com.myspringproject.service.ClientService;


@RestController
public class ClientController {

    @Autowired
    ClientService clientService;

    @RequestMapping(method=RequestMethod.POST, value="/client", consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Client> save(@RequestBody Client client){
        System.out.println("Call save ...");

        Client clientSaved = clientService.saveBLA(client);

        return new ResponseEntity<>(clientSaved,HttpStatus.CREATED);
    }

    @RequestMapping(method=RequestMethod.GET, value="/client", produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Client>> findAll(){
        System.out.println("Call findAll ...");

        Collection<Client> clientList= clientService.findAllBLA();
        return new ResponseEntity<>(clientList, HttpStatus.OK);

    }

    @RequestMapping(method=RequestMethod.DELETE, value="/client/{id}")
    public ResponseEntity<Client> delete(@PathVariable Integer id){
        System.out.println("Call delete ...");

        Client clientFound = clientService.findOneBLA(id);

        if (clientFound == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        clientService.deleteBLA(clientFound);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(method=RequestMethod.PUT, value="/client", consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Client> update(@RequestBody Client client){
        System.out.println("Call update ...");

        Client clientSaved = clientService.saveBLA(client);

        return new ResponseEntity<>(clientSaved,HttpStatus.OK);
    }
}

application.properties

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url= jdbc:postgresql://localhost:5432/clientdb
spring.datasource.username=postgres
spring.datasource.password:postgres

的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>br.com.myspringproject</groupId>
    <artifactId>MySpringProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MySpringProject</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</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-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1206-jdbc42</version>
        </dependency>
    </dependencies>

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


</project>

你怎么看我没有实现@Configuration / @Bean,因为我从Spring读到了这个文档:

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-configure-a-datasource

&#34; spring.jpa.hibernate.ddl-auto是一个特殊情况,它具有不同的默认值,具体取决于您使用的是嵌入式数据库(create-drop)还是不使用(无)。使用的方言也会根据当前的DataSource自动检测,但是如果你想要明确并且在启动时绕过该检查,你可以自己设置spring.jpa.database。&#34;

我真的不明白缺少什么。有人能帮助我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

以下帮助我解决了问题

在POM文件中将版本1.5.2.RELEASE更改为1.4.2.RELEASE,如下所示

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <!--  <version>1.5.2.RELEASE</version>--> 
    <relativePath /> <!-- lookup parent from repository -->
</parent>
祝你好运