【问题标题】:How to edit .properties files with Spring Boot 2.5.2?如何使用 Spring Boot 2.5.2 编辑 .properties 文件?
【发布时间】:2021-06-21 17:06:12
【问题描述】:

我有 3 个 .sql 文件:

  1. drop.sql(每次启动 webapp 时重置 DBMS)

  2. schema.sql(包含 DBMS 架构)

  3. data.sql(包含一些用户和一些角色的记录)

我还有 2 个 .properties 文件

  1. application-sviluppo.properties(我在 PC 上使用)

  2. application.properties(我将它用于 SERVER)

我想用 Spring Boot 2.5.2 更新 pom.xml,但我不知道应该如何更改我的文件。

文件 application-sviluppo.properties

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.1.1.1:5432/test
spring.datasource.username=postgres
spring.datasource.password=password
# This code is deprecated but it works.
spring.datasource.schema = classpath:drop.sql, classpath:schema.sql
# This code is not deprecated, it is recommended but it does not work.
#spring.sql.init.schema-locations = classpath:drop.sql, classpath:schema.sql
logging.level.org.springframework=TRACE
spring.datasource.initialization-mode=always
spring.datasource.continue-on-error=false

文件 application.properties

spring.profiles.active=sviluppo
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=???
spring.datasource.username=???
spring.datasource.password=???
logging.level.org.springframework=TRACE
spring.datasource.initialization-mode=always
spring.datasource.continue-on-error=false

我没有列出我所做的测试和尝试,因为这个页面还不够。

我在这里读到:

https://docs.spring.io/spring-boot/docs/2.5.2/reference/html/application-properties.html#application-properties

我在下面展示了一个非常简单且非常简短的示例,如果您将其加载到 IntelliJ 上,将会澄清问题。

项目下载链接:

https://easyupload.io/yovn1a

问题:

# This code is deprecated but it works.
spring.datasource.schema = classpath:drop.sql, classpath:schema.sql
# This code is not deprecated, it is recommended but it does not work.
#spring.sql.init.schema-locations = classpath:drop.sql, classpath:schema.sql

DemoApplication.java

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
@SpringBootApplication
@Profile("sviluppo")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </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.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>gestioneutenti</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <packagingExcludes>
                            WEB-INF/classes/it/applicazionijava/gestioneutenti/CrittografarePassword.class,
                            WEB-INF/classes/application-sviluppo.properties,
                            WEB-INF/classes/templates/pagineapplicazione.html
                        </packagingExcludes>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

schema.sql

CREATE TABLE IF NOT EXISTS utenti (
    id BIGSERIAL NOT NULL,
    nome VARCHAR(100) NOT NULL,
    password VARCHAR(255) NOT NULL,
    CONSTRAINT utenti_pk PRIMARY KEY(id),
    CONSTRAINT utenti_uk UNIQUE (nome)
);

CREATE TABLE IF NOT EXISTS persistent_logins (
    username VARCHAR(100) NOT NULL,
    series VARCHAR(64) PRIMARY KEY,
    token VARCHAR(64) NOT NULL,
    last_used TIMESTAMP NOT NULL
);

data.sql

BEGIN TRANSACTION;
INSERT INTO utenti (nome, password)
VALUES ('kkkkkkk', 'kkkkkkkkkkkkkkkkkkkkkkkkkk') ON CONFLICT DO NOTHING;
COMMIT;

drop.sql

DROP TABLE IF EXISTS utenti CASCADE;

【问题讨论】:

  • 从你的问题中我不清楚你想要做出什么改变。如果是关于已弃用的 DataSource 初始化属性,它们在spring.sql.init.* 中有替换,如in the release notes 所述。
  • 我做了很多测试,我都不记得了。我唯一无法修复的是这行代码: spring.datasource.schema = classpath: drop.sql, classpath: schema.sql 我无法在开发中加载 drop.sql 和 schema.sql,而在生产中只能加载 schema.sql (应用程序。属性)。弃用的解决方案有效(目前我已经离开了弃用的版本)。
  • spring.sql.init.schema-locationsspring.datasource.schema 的替代品。
  • 我认为你是对的,但如果我写“spring.sql.init.schema-locations = classpath: drop.sql, classpath: schema.sql”,我的 webapp 将无法工作。 Spring Boot 2 有一个错误。如果我使用您建议 drop.sql 文件未加载的代码。差不多一个月前我打开了这个帖子,但还没有收到解决方案。遗憾的是 Spring Boot 没有专门的论坛,也没有可以用来报告 bug 的电子邮件。
  • 提供minimal, reproducible, example 是一种好方法,可以让人们更轻松地帮助您并更快地获得问题的答案。

标签: java spring


【解决方案1】:

您正在混合配置 DataSource 初始化的已弃用 spring.datasource.* 属性及其新的 spring.sql.init.* 替换。为了向后兼容,如果您使用任何不推荐使用的属性,所有新属性都将被忽略。要迁移到新的 spring.sql.init.* 属性,您应该更新所有已弃用的 spring.datasource.* 属性以使用它们的替代品。

在您的两个属性文件中,您配置了以下不推荐使用的属性:

spring.datasource.initialization-mode=always
spring.datasource.continue-on-error=false
spring.datasource.schema=classpath:drop.sql, classpath:schema.sql

在这两个文件中,应将其替换为以下内容:

spring.sql.init.mode=always
spring.sql.init.continue-on-error=false
spring.sql.init.schema-locations=classpath:drop.sql, classpath:schema.sql

这会让你的两个文件看起来像这样:

application.properties
spring.profiles.active=sviluppo
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=???
spring.datasource.username=???
spring.datasource.password=???
logging.level.org.springframework=INFO
spring.sql.init.mode=always
spring.sql.init.continue-on-error=false
application-sviluppo.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.1.1.1:5432/test
spring.datasource.username=postgres
spring.datasource.password=password
logging.level.org.springframework=TRACE
spring.sql.init.mode=always
spring.sql.init.continue-on-error=false

【讨论】:

  • 我很惊讶。您的解决方案与我最初尝试的解决方案完全相同。我用过“;”首先和后来也“,”,但我没有解决。我在另一台计算机上使用 Spring Boot 2.5.1 完成了这个测试。我无语。我会发疯的,我很困惑。您的解决方案完美运行。我无法给自己一个解释。感谢您的耐心等待,我很抱歉让您失去了所有的时间。我真的很惭愧。
猜你喜欢
  • 1970-01-01
  • 2017-09-07
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2021-09-17
  • 2021-05-27
  • 2018-06-20
相关资源
最近更新 更多