Spring Boot 不会从 application.properties 为数据库创建自动配置

Posted

技术标签:

【中文标题】Spring Boot 不会从 application.properties 为数据库创建自动配置【英文标题】:Spring Boot doesn't create automatic configuration for database from application.properties 【发布时间】:2016-09-14 04:07:22 【问题描述】:

我遵循a guide 使用 JPA 创建简单的 Spring Boot 应用程序,当应用程序完成后,它应该根据 application.properties 文件中的配置创建数据库连接,但是它没有发生(我得到 不是托管类型 例外)。我知道这是 application.properties 文件的问题,因为当我像here 一样手动配置它时,应用程序可以正常运行。

项目结构:

POM.xml:

<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>demo.tomek</groupId>
<artifactId>SpringMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>


</dependencies>

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

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


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

application.properties 文件:

spring.datasource.url = jdbc:mysql://localhost:3306/TEST?createIfNotExists=true
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=but

spring.jpa.show-sql=true

# Enable spring data repos 
spring.data.jpa.repositories.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update

spring.thymeleaf.cache=false
spring.template.cache=false

static void main()

@SpringBootApplication
@ComponentScan("demo")
@EnableJpaRepositories("demo")
public class SpringBootTEST 

    public static void main(String[] args) 
        ApplicationContext ctx = SpringApplication.run(SpringBootTEST.class, args);

        ProductService pRepo = ctx.getBean("productService", ProductService.class);
        pRepo.addSomeProducts();
        CustomerService cRepo = ctx.getBean("customerService", CustomerService.class);
        cRepo.addSomeCustomers();
    

让我再次强调一下:当我像这里一样手动配置时一切正常:http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/

我的想法用完了......

【问题讨论】:

将你的 SpringBootTEST 类移动到 demo 包中删除除 @SpringBootApplication 之外的所有注释并重新启动。 @M.Deinum 我试过了......它也有效:) 【参考方案1】:

由于您的实体不在您的应用程序类的子包中,您还必须在SpringBootTEST 中添加以下注释,以便 Hibernate 可以扫描您的实体类:

@EntityScan("demo.database")

或者,如果您在 SpringBootApplication 注释中指定包,它也应该可以工作:

@SpringBootApplication("demo")

那么您还应该能够删除自定义的@ComponentScan@EnableJpaRepositories 注释。

【讨论】:

我在想@ComponentScan 会处理它。我也尝试删除其他注释,但没有成功,我不得不将它们保留原样。

以上是关于Spring Boot 不会从 application.properties 为数据库创建自动配置的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 不会从 application.properties 为数据库创建自动配置

无法集成测试 gradle 多模块 Spring-Boot-Application

Spring Boot 单元测试未检测到自动装配组件的模块

如果从计划的作业中调用,Spring Boot 存储库不会保存到数据库

点燃不会从 spring-boot 2.0.5 开始 - h2 属性 NESTED_JOINS 不存在

内存数据库H2中的Spring Boot在初始化时不会从文件中加载数据