๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] EnvironmentCapable - Profile ์‚ฌ์šฉํ•˜๊ธฐ

by Leica 2020. 3. 4.
๋ฐ˜์‘ํ˜•

[Spring] EnvironmentCapable - Profile ์‚ฌ์šฉํ•˜๊ธฐ

Spring์˜ ApplicationContext๋Š” BeanFactory ๊ธฐ๋Šฅ๋งŒ ํ•˜๋Š”๊ฑด ์•„๋‹ˆ๋‹ค.

 

ApplicationContext๊ฐ€ ์ƒ์†๋ฐ›๋Š” ๋‹ค์–‘ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋“ค

 

ApplicationContext๊ฐ€ ์ƒ์†๋ฐ›๋Š” ๋‹ค์–‘ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋“ค ์ค‘ EnvironmentCapable ์ธํ„ฐํŽ˜์ด์Šค๋Š” 'Profile(ํ”„๋กœํŒŒ์ผ)' ์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

 

Profile(ํ”„๋กœํŒŒ์ผ)์˜ ๊ฐœ๋…

ํ”„๋กœํŒŒ์ผ = ๋นˆ๋“ค์˜ ๋ฌถ์Œ

 

ํ”„๋กœํŒŒ์ผ์€ ํŠน์ • ์‹คํ–‰ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ๋นˆ๋“ค์˜ ๋ฌถ์Œ์ด๋‹ค.

ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ๋นˆ ๋ฌถ์Œ๊ณผ ํ”„๋กœ๋•์…˜(์šด์˜) ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ๋นˆ ๋ฌถ์Œ์ด ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

๋˜ ๊ฐ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ๋นˆ๋“ค์„ ์จ์•ผํ•˜๋Š” ๊ฒฝ์šฐ, ํŠน์ • ํ™˜๊ฒฝ์—์„œ๋งŒ ๋“ฑ๋กํ•ด์•ผํ•˜๋Š” ๋นˆ๋“ค์ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœํŒŒ์ผ์€ ๊ทธ๋Ÿฌํ•œ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.


ํ”„๋กœํŒŒ์ผ ํ™•์ธํ•˜๊ธฐ - Environment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
 
@Component
public class AppRunner implements ApplicationRunner {
 
    @Autowired
    ApplicationContext ctx;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        Environment environment = ctx.getEnvironment();
        System.out.println(Arrays.toString(environment.getActiveProfiles()));
        System.out.println(Arrays.toString(environment.getDefaultProfiles()));
    }
}
cs

 

ApplicationContext์˜ getEnvironment()๋กœ Environment ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ Environment์˜ ์—ญํ• 

ํ™œ์„ฑํ™” ํ•  ํ”„๋กœํŒŒ์ผ ํ™•์ธ ๋ฐ ์„ค์ •

 

์‹คํ–‰ ๊ฒฐ๊ณผ

Environment์˜ getActiveProfiles()๋กœ ํ˜„์žฌ activeํ•œ ํ”„๋กœํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ณ  getDefaultProfiles()๋กœ๋Š” ์–ด๋–ค ํ”„๋กœํŒŒ์ผ์ด๋“  ์ƒ๊ด€ ์—†์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉ๋˜๋Š” ํ”„๋กœํŒŒ์ผ์„ ๊ฐ€์ ธ์˜จ๋‹ค.

 

ํ”„๋กœํŒŒ์ผ ์ •์˜ํ•˜๊ธฐ - @Configuration ํด๋ž˜์Šค์— ์ •์˜

ํ”„๋กœํŒŒ์ผ์€ @Profile ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํด๋ž˜์Šค, ๋ฉ”์†Œ๋“œ์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋นˆ ์„ค์ •ํŒŒ์ผ์„ ์ƒˆ๋กœ ์ž‘์„ฑํ•ด๋ณด์ž.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
 
@Configuration
@Profile("test")
public class TestConfiguration {
 
    @Bean
    public BookRepository bookRepository() {
        return new TestBookRepository();
    }
}
cs

 

@Profile("test")์™€ ๊ฐ™์ด ์ง€์ •ํ•ด์คŒ์œผ๋กœ์จ ์ด ๋นˆ ์„ค์ •ํŒŒ์ผ์€ test ํ”„๋กœํŒŒ์ผ์ผ๋•Œ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค.

์ฆ‰ test ํ”„๋กœํŒŒ์ผ๋กœ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์œผ๋ฉด ์ด ๋นˆ ์„ค์ •ํŒŒ์ผ์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

ApplicationRunner์—์„œ ์œ„ ์„ค์ •ํŒŒ์ผ์— ๋“ฑ๋ก๋œ ๋นˆ์„ ์ฃผ์ž…๋ฐ›์œผ๋ ค๊ณ  ํ•ด๋ณด์ž.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
 
@Component
public class AppRunner implements ApplicationRunner {
 
    @Autowired
    ApplicationContext ctx;
 
    // BookRepository๋ฅผ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์—†๋‹ค.
    @Autowired
    BookRepository bookRepository;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        Environment environment = ctx.getEnvironment();
        System.out.println(Arrays.toString(environment.getActiveProfiles()));
        System.out.println(Arrays.toString(environment.getDefaultProfiles()));
    }
}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ

BookRepository ํƒ€์ž…์˜ ๋นˆ์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

test ํ”„๋กœํŒŒ์ผ๋กœ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ BookRepository ๋นˆ์„ ๋“ฑ๋กํ•œ ์„ค์ •ํŒŒ์ผ์ด ์ฝํžˆ์ง€ ์•Š์•˜๊ณ  ๋”ฐ๋ผ์„œ BookRepository ๋นˆ๋„ ๋“ฑ๋ก๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

ํ”„๋กœํŒŒ์ผ ์„ค์ •ํ•˜๊ธฐ

1) Active profiles ์„ค์ •

IDE์˜ ์‹คํ–‰ ํ™˜๊ฒฝ ์„ค์ •์—์„œ Active profiles์— ํ”„๋กœํŒŒ์ผ์„ ์„ค์ •ํ•œ๋‹ค.

 

2) VM Options ์„ค์ •

Active profiles ์„ค์ • ํ•ญ๋ชฉ์ด ์—†์„ ๊ฒฝ์šฐ VM options์— ์„ค์ •ํ•ด๋„ ๋œ๋‹ค.

 

-Dspring.profiles.active="ํ”„๋กœํŒŒ์ผ1,ํ”„๋กœํŒŒ์ผ2,ํ”„๋กœํŒŒ์ผ3,..."

 


IDE์—์„œ active profiles์„ 'test'๋กœ ์„ค์ •ํ•˜๊ณ  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‹ค์‹œ ์‹คํ–‰ํ•ด๋ณด์ž.

 

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉฐ getActiveProfiles()๋กœ test๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

Active profile์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•˜์œผ๋ฏ€๋กœ ๋‚˜๋จธ์ง€ ํ”„๋กœํŒŒ์ผ ์ •์˜ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

 

ํ”„๋กœํŒŒ์ผ ์ •์˜ํ•˜๊ธฐ - ๋‚˜๋จธ์ง€ ๋ฐฉ๋ฒ•๋“ค

1) @Bean ๋ฉ”์†Œ๋“œ์— ์ •์˜

@Bean ๋ฉ”์†Œ๋“œ์— @Profile์„ ๊ฐ™์ด ๋ถ™์—ฌ์„œ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
 
@Configuration
public class TestConfiguration {
 
    @Bean @Profile("test")
    public BookRepository bookRepository() {
        return new TestBookRepository();
    }
}
cs

 

2) @Component ํด๋ž˜์Šค์— ์ •์˜

@Component ํด๋ž˜์Šค์— @Profile์„ ๊ฐ™์ด ๋ถ™์—ฌ์„œ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
 
@Repository
@Profile("test")
public class TestBookRepository implements BookRepository {
}
cs

 

References

์ธํ”„๋Ÿฐ - ๋ฐฑ๊ธฐ์„ ๋‹˜์˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ํ•ต์‹ฌ ๊ธฐ์ˆ 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€