2017-03-14 46 views
1

我是用其他版本(1.5.2.RELEASE)时,我得到这些错误信息如下弹簧引导版本1.3.5教程:Bean方法“数据源”没有加载

*************************** 
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因为我这个文件从春天阅读:

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

“的spring.jpa.hibernate.ddl-auto是一种特殊情况,因为它具有不同的默认值,具体取决于您是使用嵌入式数据库(创建 - 删除)还是不使用(无)。使用方言也会自动检测到根据当前的数据源,但你可以设置,如果你想明确并在启动时检查旁路spring.jpa.database自己。”

我真的不明白什么是失踪。有人能帮帮我吗?谢谢。

+0

哪里是你的'application.properties'目录......它必须放在src/main/resources'而不是'src/main/java'中 –

+0

它在src/main /资源 – Pep

回答

0

继帮我解决这个问题

更改版本1.5.2.RELEASE1.4.2.RELEASE在POM文件中像下面显示

<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> 

好路ck

相关问题