当前位置: 首页 > 知识库问答 >
问题:

Bean方法'dataSource'未加载

孟雪风
2023-03-14

我在使用其他版本(1.5.2.RELEASE)学习spring boot 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

波姆。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-配置数据

"spring.jpa.hibernate.ddl-autoa是一个特例,因为它具有不同的默认值,具体取决于您使用的是嵌入式数据库(create-drop)还是不使用(无)。要使用的方言也会根据当前的DataSource自动检测到但如果您想显式并在启动时绕过该检查,您可以自行设置spring.jpa.database。"

我真的不明白少了什么。有人能帮我吗?非常感谢。

共有1个答案

酆晔
2023-03-14

跟踪帮助我解决了这个问题

更改版本1.5.2。将发布到1.4.2。在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>

祝你好运

 类似资料:
  • 我对Spring Boot很陌生,正在尝试做一些测试。当我突然遇到一个我在过去三个小时一直试图解决的问题时... 我认为这里的主要问题是: 原因:org . spring framework . beans . factory . beancreationexception:创建名为“org . spring framework . boot . auto configure . flyway .

  • 我已经升级到spring Boot2.1版本,我在启动应用程序时遇到了奇怪的异常。 [O.S.B.W.S.C.AnnotationConfigServletWebServerApplicationContext]上下文初始化期间遇到异常-取消刷新尝试:org.SpringFramework.Beans.Factory.Support.BeanDefinitionOverrideException:

  • 本文向大家介绍详解Spring中Bean的加载的方法,包括了详解Spring中Bean的加载的方法的使用技巧和注意事项,需要的朋友参考一下 之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean的解析复杂些,从之前的例子开始. Spring中加载一个bean的方式: 来看看getBean(String name)方法源码, 该getBean(String name)方法位于Abstra

  • 问题内容: 这不会编译,以下方法只会写出我添加的最新项目,而不会追加到以前的条目中。我究竟做错了什么? 问题答案: 调用方法的事实并不意味着它会更改正在打开的文件的模式。 你还需要以附加模式打开文件: 另请注意,文件将以系统默认编码写入。并非总是如此,它可能会导致互操作性问题,你可能需要明确指定文件编码。

  • IM使用Oracle数据库 这是我的豆子: id喜欢改变当前模式使用Spring. in sql查询看起来像: 设置CURRENT_SCHEMA名称。 我可以设置一些初始查询或一些参数吗?如何解决这个问题?

  • 1. Tomcat JDBC DBCP作为老牌DataSource一统江湖很久,也很久没有更新了。但Tomcat JDBC 最近出来把它替换掉了,理由详见Tomcat JDBC的自述,简单来说你又慢,又复杂。 所幸Tomcat JDBC完全兼容DBCP的旧属性,只要把spring配置文件里的class 名改掉就可以继续用了。 <bean id="dataSource" class="org