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因爲我這個文件從春天閱讀:
「的spring.jpa.hibernate.ddl-auto是一種特殊情況,因爲它具有不同的默認值,具體取決於您是使用嵌入式數據庫(創建 - 刪除)還是不使用(無)。使用方言也會自動檢測到根據當前的數據源,但你可以設置,如果你想明確並在啓動時檢查旁路spring.jpa.database自己。」
我真的不明白什麼是失蹤。有人能幫幫我嗎?謝謝。
哪裏是你的'application.properties'目錄......它必須放在src/main/resources'而不是'src/main/java'中 –
它在src/main /資源 – Pep