1 Nisan 2022 Cuma

Spring Profiles Kullanımı

Giriş
Açıklaması şöyle. Özellikle test kodlarında çok işe yarar.
Spring Boot provides profiles that help to create environment-specific configurations and stereotypes in the applications. 
Kullanılabilecek anotasyonlar şöyle

1. Spring boot 3.0.x ve Ötesi
Açıklaması şöyle
In spring boot 3.0.x onwards all the deprecated methods of 2.7.x have been removed. spring.profiles.include one of them that has been removed from spring boot. Instead of spring.profiles.include, spring boot has introduced spring.profiles.group in spring 2.4.x onwards. you have to make multiple groups based on profiles like spring.profiles.group.{env}={profile1},{profile2} in application.properties file.
Açıklaması şöyle
spring.profiles={env} is also removed, use spring.config.activate.on-profile={env}.
Örnek
Şöyle yaparız. Çalışılan ortam dev veya prod ise belirtilen profile dosyalar yüklenir
spring.profiles.group.dev=common
spring.profiles.group.prod=common
Yani ortam dev ise
application-common.properties
application-dev.properties
yüklenir

Örnek
Açıklaması şöyle
Profile Groups is another feature added in Boot 2.4. using this feature you can have multiple profiles activated under one group of profiles.

spring.profiles.group.development[0]=devdb 
spring.profiles.group.development[1]=devkafka

The application can now be started using --spring.profiles.active=development to active the development, devdb and devkafka profiles in one hit.
2. Spring boot 2.4.x - 2.7.x
Açıklaması şöyle. Yani eskisi gibi çalışabilmek mümkün
In spring boot 2.4.x spring.profiles.include marked as deprecated which will be removed in spring boot 3.0.0

To support the previous configuration, you can add the below line in application.properties.

spring.config.use-legacy-processing=true
3. Spring boot 2.3.x veya Öncesi
Active profile ismi verilir. Aktif profile isimleri birden fazla verilebilir ve virgül ile ayrılır. 4 yöntem var. 
1. Komut Satırı --spring.profiles.active kullanımı
2. Komut Satırı -D kullanımı
3. envrironment variables
4. application.properties dosyası

Örnek
Eğer active profile yanında ortak bir dosyayı daha yüklemek istersek spring.profiles.include kullanılır. Şöyle yaparız
spring.profiles.include=common
spring.profiles.active=dev
Böylece şu dosyalar yüklenebilir.
application-common.properties
application-dev.properties


1. Komut Satırı - --spring.profiles.active
--spring.profiles.active seçeneği kullanılır. 
Örnek
Şöyle yaparız.
start "Service 1" /B java.exe -jar service1.jar --spring.profiles.active=local
2. Komut satırı - java -Dspring.profiles.active
Aslında --spring.profiles.active ile -D--spring.profiles.active temelde aynı şeyler.
Örnek
Şöyle yaparız
java -jar -Dspring.profiles.active=dev application.jar
# or
java -jar application.jar --spring.profiles.active=dev
Örnek
Şöyle yaparız. Burada dev ve container isimli iki tane profile etkinleştiriliyor.
java -Dspring.profiles.active=dev,container -jar build/libs/dockeriser-0.0.1-SNAPSHOT.jar
3. environment variables
Örnek
Şöyle yaparız
export spring_profiles_active=dev
Örnek
environment variables koddan da verilebiliyor. Şöyle yaparız
@Autowired 
private ConfigurableEnvironment env; 
... 
env.setActiveProfiles("dev");

4. application.properties
Örnek
Profil vermek için şöyle yaparız.
spring.profiles.active=dev,local
Örnek - YAML Dosyası
Şöyle yaparız.
spring:
  profiles:
    active: dev
Açıklaması şöyle
If you prefer to have only one configuration (application.yml) file in your project, then you can separate each profile configuration using three dashes.

If you see in the above example, there are three sections, which are separated by three dashes. First section is for common properties which are enabled for all the spring profiles. Second section is for integration-test spring profile and third one is for prod profile.

Hiç yorum yok:

Yorum Gönder