简体   繁体   English

application.properties弹簧启动值注入

[英]application.properties spring boot value injection

I'm working on the REST API with spring boot. 我正在使用Spring Boot开发REST API。 I want to use git in my project. 我想在项目中使用git。 in the file application.properties I have the database Url, username and password that I do not want to push on git. 在文件application.properties中,我具有不想在git上推送的数据库Url,用户名和密码。 I don't know how can I create a file which contains my database configuration and how to inject those configurations in the application.properties . 我不知道如何创建一个包含数据库配置的文件,以及如何将这些配置注入application.properties。

application.properties application.properties

## Server Properties
server.port= 5000

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url= jdbc:mysql://localhost:3306/MyApp?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username= user
spring.datasource.password= pass

Spring picks up configuration properties not only from the application.properties but also from command line arguments, JAVA System-properties or from environmental-variables. Spring不仅从application.properties中获取配置属性,还从命令行参数,JAVA System属性或环境变量中获取配置属性。

See complete list here: Spring Externalized Configuration . 请参阅此处的完整列表: Spring外部化配置

So - for reference - you can keep the properties in the application.properties file with some default values (like in your example) in order to let other users know what kind of properties they can set for your application. 因此,作为参考,您可以使用一些默认值(例如,在您的示例中)将application.properties文件中的属性保留下来,以便让其他用户知道他们可以为您的应用程序设置什么样的属性。

But instead of setting your real values there, you can either pass the variable to your application as arguments, like 但是,您可以将变量作为参数传递给应用程序,而不必在此处设置实际值,例如

-Dspring.datasource.username=user -Dspring.datasource.password= pass

or you can set them as environmental variables. 或者您可以将它们设置为环境变量。

You can even create multiple configuration with different settings. 您甚至可以使用不同的设置创建多个配置。 If Spring cannot find a variable in the current configuration, then it will pick it up from application.properties (or from the other sources - see above) 如果Spring在当前配置中找不到变量,那么它将从application.properties(或其他来源-参见上文)中选择它。

first you should add application.properties to .ignore file like this 首先,您应该像这样将application.properties添加到.ignore文件

application.properties 

if you will just connect to database you won't need to inject values by hand you just write it in application.properties 如果您仅连接到数据库,则无需手动注入值,只需将其写在application.properties中
but if you want to put values in properties file and use it in Application 但是如果您想将值放入属性文件中并在Application中使用它

package com.microservice.test.limitservice;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("limit-service")
public class Configuration {
private int minimum;
private int maximum;

public int getMinimum() {
    return minimum;
}
public void setMinimum(int minimum) {
    this.minimum = minimum;
}
public int getMaximum() {
    return maximum;
}
public void setMaximum(int maximum) {
    this.maximum = maximum;
}

}

and how to inject it simply 以及如何简单地注入

@Autowired
private Configuration configuration;

the application.properties file could be like this application.properties文件可能像这样

limit-service.minimum=56333445
limit-service.maximum=6500

you should notice that it start with as example limit-service and @ConfigurationProperties("**limit-service**") 您应该注意到它以示例limit-service和@ConfigurationProperties("**limit-service**")开头

And if you want to store your configuration in application.properties secure you can see this link Spring Boot how to hide passwords in properties file 如果您想将配置存储在安全的application.properties中,则可以看到此链接Spring Boot如何在属性文件中隐藏密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot application.properties 值未填充 - Spring Boot application.properties value not populating Spring Boot application.properties - Spring Boot application.properties spring 引导中的@value 未从 application.properties 提供值 - @value in spring boot not giving value from application.properties 如何在 spring 引导中读取构造函数内的 application.properties 值? - How to read application.properties value inside the constructor in spring boot? Spring 引导项目未绑定来自 application.properties 的 @Value - Spring Boot project not binding @Value from application.properties 在 Spring Boot 中访问 application.properties 文件中定义的值时出现问题 - problem access a value defined in the application.properties file in Spring Boot 具有不同application.properties的Spring Boot应用程序 - Spring Boot Application with different application.properties Spring 引导中的配置文件特定 application.properties 文件和多个配置文件的默认值注入 - Profile specific application.properties file in Spring boot and default injection of values for multiple profiles Spring boot 访问组件中的 application.properties - Spring boot access application.properties in components 带有 Oracle NoSql 的 Spring 启动 application.properties - Spring boot application.properties with Oracle NoSql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM