有人可以显示正确的格式以用于在Flyway迁移中使用application.properties配置。
我想在我的application.properties文件中使用数据源配置的用户名来授予数据库表的权限(使用Flyway进行数据库迁移,用户名最终会因环境而异),但是我找不到语法的示例。
示例application.properties:
# Database
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/example_db
spring.datasource.username=example_db_application
spring.datasource.password=examplePassword1
Run Code Online (Sandbox Code Playgroud)
移民:
CREATE TABLE token
(
id TEXT,
value TEXT,
);
GRANT SELECT, INSERT, UPDATE, DELETE ON token TO ${spring.datasource.username};
Run Code Online (Sandbox Code Playgroud)
我尝试了各种迭代(flyway.placeholders.spring.datasource.username,尝试不指定前缀:spring.flyway.placeholder-prefix =),但是没有运气。
我有几个 Spring Boot 应用程序。它们都是 maven 项目,并且在所有项目中,我都有针对不同环境的单独配置文件,例如 application.properties、application-dev.properties、application-uat.properties。当我从 IDE 运行应用程序时,它有时从 application.properties 文件中读取,有时从 application-dev.properties 文件中读取。我使用 IntelliJ IDEA 进行开发,但我一直无法弄清楚是什么决定了要读取哪个文件。是依赖于 IDE 还是 Spring 中有一些配置决定选择哪个属性文件?
我试图从包含所有应用程序属性的github存储库中获取数据库配置。下图说明了我所拥有的服务的结构:
当我访问我的配置服务器微服务时,一切正常,即使我在邮递员中对其进行测试,也可以恢复配置。
当我尝试运行产品服务同时指向配置服务器微服务以获取数据库配置时,问题就来了。我收到以下错误:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2018-11-17 17:53:23.730 INFO 9497 --- [ restartedMain] o.a.s.p.ProductServicesApplication : Starting ProductServicesApplication on …Run Code Online (Sandbox Code Playgroud) 我有以下docker compose文件,用于Spring boot container连接到 a 的 a mongoDB container:
#
# APIs
#----------------------------------------------
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
#
# Utilities
#----------------------------------------------
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerStats
Run Code Online (Sandbox Code Playgroud)
在我的春季靴子中,application.properties我有:
server.port=8080
spring.main.allow-bean-definition-overriding=true
spring.data.mongodb.uri=mongodb://admin:admin@mongo:27017/pokerStats
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=pokerStats
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 Postman 到达端点时,出现以下错误:
{
"timestamp": "2020-04-07T17:39:19.129+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='pokerStats', password=<hidden>, mechanismProperties=<hidden>}; nested …Run Code Online (Sandbox Code Playgroud) mongodb docker spring-boot docker-compose application.properties
我在数据源密码字段中有“(双引号)、@、~、!、%、&、}、] 等特殊字符。当我运行我的 springboot 应用程序并尝试连接到我遇到的数据库时 -
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "problem accessing trust store"
Caused by: java.security.KeyStoreException: problem accessing trust store
Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect
Caused by: java.security.UnrecoverableKeyException: Password verification failed
Run Code Online (Sandbox Code Playgroud)
我的问题是:
(1) 有没有办法查看 Spring 使用什么密码来尝试连接?
(2) 我需要转义密码中的任何特殊字符application.properties吗?
我能够使用独立的 java 程序连接到数据库,但我不得不转义密码中的双引号。我在后端使用 Springboot-2.0.4、Flyway-5.2.4、jdk 1.8 和 MS-SQL 服务器。
谢谢。
我有这些属性在我的application.properties:
spring.datasource.url=jdbc:postgresql://localhsost:5432/myDatabase
spring.datasource.username=myUsername
Run Code Online (Sandbox Code Playgroud)
我想mvn test使用上述以外的其他值运行,例如:
spring.datasource.url=jdbc:postgresql://my.test.server.com:5432/myDatabase
spring.datasource.username=anotherUsername
Run Code Online (Sandbox Code Playgroud)
我尝试了以下
mvn test -Drun.arguments='--spring.datasource.jdbc:postgresql://my.test.server.com:5432/myDatabase --spring.datasource.username=anotherUsername'
Run Code Online (Sandbox Code Playgroud)
并且没有spring前缀:
mvn test -Drun.arguments='--datasource.jdbc:postgresql://my.test.server.com:5432/myDatabase --datasource.username=anotherUsername'
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。如何覆盖application.properties运行上下文中的值mvn test?
我正在尝试使用以下代码从 spring 的 application.properties 解析 LocalTime:
@Value("#{ T(java.time.LocalTime).parse('${app.myDateTime}')}")
private LocalTime myDateTime;
Run Code Online (Sandbox Code Playgroud)
在 application.properties 中,我定义了这样的属性:
app.myDateTime=21:45:00
Run Code Online (Sandbox Code Playgroud)
错误信息:
Failed to bind properties under 'app.my-date-time' to java.time.LocalTime:
Property: app.my-date-time
Value: 21:45:00
Origin: class path resource [application.properties]:44:15
Reason: failed to convert java.lang.String to @org.springframework.beans.factory.annotation.Value java.time.LocalTime
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?谢谢你。
调试模式错误:
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalTime] for value '21:45:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [21:45:00]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.boot.context.properties.bind.BindConverter$CompositeConversionService.convert(BindConverter.java:170)
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:96)
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:88)
at org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:313) …Run Code Online (Sandbox Code Playgroud) 我有基本的 SpringBoot 应用程序。我按照本教程:https://www.baeldung.com/spring-boot-h2-database从头开始创建所有内容,但是当我完成所有步骤并尝试访问 h2-console 时。它向我显示“无法访问此站点\xe2\x80\x99”。首先,我尝试了默认路径:http://localhost:8080/h2-console,然后我添加到我的 application.properties 文件中 - server.contextPath="api" 和 server.port=8090,以确保该端口未被使用,但仍然无法访问 h2 控制台。
\n这是我的 pom.xml 文件:
\n<parent>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-parent</artifactId>\n <version>2.3.3.RELEASE</version>\n <relativePath/> <!-- lookup parent from repository -->\n\n</parent>\n\n<groupId>gtech</groupId>\n<artifactId>agriculturerent</artifactId>\n<version>0.0.1-SNAPSHOT</version>\n\n<properties>\n <java.version>1.8</java.version>\n <maven.compiler.source>1.8</maven.compiler.source>\n <maven.compiler.target>1.8</maven.compiler.target>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n</properties>\n\n<dependencies>\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-data-jpa</artifactId>\n </dependency>\n\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-test</artifactId>\n <scope>test</scope>\n <exclusions>\n <exclusion>\n <groupId>org.junit.vintage</groupId>\n <artifactId>junit-vintage-engine</artifactId>\n </exclusion>\n </exclusions>\n </dependency>\n\n <dependency>\n <groupId>com.h2database</groupId>\n <artifactId>h2</artifactId>\n <scope>runtime</scope>\n </dependency>\n\n <dependency>\n <groupId>org.hibernate</groupId>\n <artifactId>hibernate-core</artifactId>\n <version>5.4.20.Final</version>\n </dependency>\n\n</dependencies>\n\n<build>\n <plugins>\n <plugin>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-maven-plugin</artifactId>\n </plugin>\n </plugins>\n</build>\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的 application.properties 文件:
\nspring.datasource.url=jdbc:h2:file:./TestDb\nspring.datasource.driverClassName=org.h2.Driver\nspring.datasource.username=sa\nspring.datasource.password=""\nspring.jpa.database-platform=org.hibernate.dialect.H2Dialect\n\nspring.h2.console.enabled=true\nRun Code Online (Sandbox Code Playgroud)\n主类是默认类:
\n@SpringBootApplication\npublic class AgriculturerentApplication {\n\n public …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我需要使用两个MongoDB数据库。我不知道如何在spring应用程序的application.properties文件中添加2个MongoDB数据库。
这是我项目的application.properties文件,
spring.data.mongodb.database=DB1
spring.data.mongodb.authentication-database=DB1
spring.data.mongodb.host=dev-ng-mongo1.domain.com
spring.data.mongodb.password=9876512
spring.data.mongodb.port=27017
spring.data.mongodb.username=pavan
Run Code Online (Sandbox Code Playgroud)
但我想将另一个MongoDB数据库用于同一项目。如何在application.properties文件中添加新数据库。
java configuration mongodb spring-boot application.properties
在我的 Spring Boot 2.4.3 应用程序中,我使用 Testcontainers 并按照互联网上的说明进行操作。我有一个application.yaml:
spring:
datasource:
url: jdbc:tc:postgresql:13.2:///testdb?TC_INITSCRIPT=tc_initscript_postgresql.sql
username: duke
password: s3crEt
jpa:
database-platform: org.hibernate.dialect.PostgreSQL95Dialect
hibernate:
ddl-auto: create-drop
Run Code Online (Sandbox Code Playgroud)
但是当我调试应用程序时,容器始终将“test”作为 URL、用户名和密码的值。
这是我的测试课:
@ActiveProfiles("test")
@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractApplicationIT {
final static DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres:13.2-alpine");
@Container
// public static GenericContainer postgreSQLContainer = new GenericContainer(POSTGRES_IMAGE)
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE)
// .withInitScript("tc_initscript_postgresql.sql")
// .withPassword("password")
// .withUsername("username")
// .withDatabaseName("test")
// .withInitScript("tc_initscript_postgresql.sql")
;
// @DynamicPropertySource
// static void postgresqlProperties(DynamicPropertyRegistry registry) {
// registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl); …Run Code Online (Sandbox Code Playgroud) 目标:从我的 Java 类中的 application.properties 文件中读取属性值。
当前行为:获取空值
预期行为:
value1
value3
Run Code Online (Sandbox Code Playgroud)
应用程序属性
category1.subcategory2=value1
category1.subcategory3=value3
greeting.salutation=Hello
Run Code Online (Sandbox Code Playgroud)
TryValueApplication.java
package com.example.tryvalue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TryValueApplication {
@Value("${category1.subcategory2}")
private String category1;
@Value("basic value")
private String category3;
public static void main(String[] args) {
SpringApplication.run(TryValueApplication.class, args);
TryValueApplication tryValueApplication = new TryValueApplication();
System.out.println("Running main method");
tryValueApplication.printCategory();
}
public void printCategory(){
System.out.println("Print environment values");
System.out.println(category1);
System.out.println(category3);
}
}
Run Code Online (Sandbox Code Playgroud)
尝试过:我尝试从 @RestController 类中访问相同的值,并且它正在工作。GreetingController.java
package com.example.tryvalue.controller;
import com.example.tryvalue.EnvironmentValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import …Run Code Online (Sandbox Code Playgroud) 我通过 Spring Initializr 创建了 Spring 项目,项目结构如下:
我在 application.properties 文件中定义了属性:
my.prop=testvalue
Run Code Online (Sandbox Code Playgroud)
我将此值注入到 MyClass 中,如下所示:
@Component
class MyClass {
@Value("${my.prop}")
private String myProp;
public String getMyProp() {
return myProp;
}
}
Run Code Online (Sandbox Code Playgroud)
ConfigBeans定义如下:
package com.example.propertiesdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigBeans {
@Bean
public MyClass myLitoBean() {
return new MyClass();
}
}
Run Code Online (Sandbox Code Playgroud)
属性demoApplication.java:
package com.example.propertiesdemo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class PropertiesdemoApplication {
public static void main(String[] args) {
ApplicationContext context
= new AnnotationConfigApplicationContext(
ConfigBeans.class);
MyClass myClass = …Run Code Online (Sandbox Code Playgroud) spring-boot ×10
java ×6
spring ×5
maven ×3
mongodb ×2
docker ×1
flyway ×1
h2 ×1
java-time ×1
properties ×1