application.properties etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
application.properties etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

17 Haziran 2019 Pazartesi

SpringBoot application.properties Ayarları

Giriş
Açıklaması şöyle.
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
Test Ortamı
Açıklaması şöyle.
We might also have a requirement to use different property values when our application is under test.

Spring Boot handles this for us by looking in our “src/test/resources” directory during a test run. Again, default properties will still be injectable as normal but will be overridden these ones if there is a collision.
spring.config.import Ayarı
Örnek
elimizde env.properties dosyası olsun. Bu dosyayı .gitignore yaparsak git'e gönderilmez.
DB_DATABASE=NameofyourappSavedIntoYourDB
DB_USER=yourUsername
DB_PASSWORD=SuperStrongPassword
API_KEY=superkey
Şöyle yaparız
spring.config.import=file:env.propertiesalo
spring.datasource.username=${DB_USER} spring.datasource.password=${DB_PASSWORD}
--spring.config.name Ayarı
application.properties dosyasını yerine başka bir isim kullanabilmemizi sağlar.

Örnek
Şöyle yaparız.
java -jar myproject.jar --spring.config.name=myproject
--spring.config.location Ayarı
Açıklaması şöyle. -Dspring.config.location seçeneği veya --spring.config.location seçeneği ile dosya yolu belirtilir.
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths).
Örnek
Şöyle yaparız
java -jar -Dspring.config.location=file:\\\C:/application-myconfig.yml aplication1.jar
Örnek
Şöyle yaparız.
java -jar app.jar --spring.config.location=file:<property_file_location>
Örnek
Şöyle yaparız.
java -jar myproject.jar --spring.config.location=classpath:/default.properties
Örnek
Kodun içinden şöyle yaparız.
@SpringBootApplication
public class SimpleBoot {

  public static void main(String[] args) {
    System.setProperty("spring.config.location","file:/path/to/application.yml")
    SpringApplication.run(SimpleBoot.class, args);
  }
}
Örnek
Kodun içinden şöyle yaparız.
static void main(String[] args) {
  SpringApplication.run(MyApplication,
    ["--spring.config.location=file:${System.getProperty('user.home')}/application.yml"] 
    + args)
}