티스토리 뷰

Application 실행 환경에 따른 설정을 위해 profile 설정을 추가합니다. 

 

스프링부트에서는 기본 설정을 application.properties에 하는데 개발 환경에 따라 달라지는 값들은 profile을 추가함으로써 동적으로 이용가능합니다. 

추가하는 방법은 간단합니다. 

application-[profile].properties 

이 규칙으로 파일을 생성하면 됩니다. 

일단 세개의 파일을 생성합니다. 

  • application-local.properties 
  • application-dev.properties 
  • application-product.properties 

내용 

application-local.properties
-----------------------------
spring.thymeleaf.cache=false
app.title=springboot-study-local
-----------------------------

application-dev.properties
-----------------------------
spring.thymeleaf.cache=false
app.title=springboot-study-dev
-----------------------------

application-product.properties
-----------------------------
spring.thymeleaf.cache=true
app.title=springboot-study-product
-----------------------------

기존에 application.properties에 "spring.thymeleaf.cache" 이 부분은 주석 처리합니다. 

 

스프링부트에서 profile 설정은 이걸로 끝입니다. 

 

테스트 controller 

@RestController
public class ProfileTestAPIController {

    @Value("${app.title}")
    private String appTitle;
    
    @GetMapping("/profile")
    public String test() {
        return appTitle;
    }
    
}

@Value 어노테이션을 이용해서 설정 값을 가져올 수 있습니다. 

 

Testcase 

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("local")
class ProfileTestAPIControllerTest {

    @Autowired 
    private MockMvc mockMvc;

    @Test
    void doubleMapping() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/profile")
                .accept(MediaType.TEXT_PLAIN)
                )
                .andExpect(status().isOk())
                .andReturn();
        assertThat(result.getResponse().getContentAsString()).isEqualTo("springboot-study-local");
    }

}

서버 실행 하기 

eclipse

java -jar -Dspring.profiles.active=local ./study-springboot-0.0.1-SNAPSHOT.jar

브라우저 확인 

스프링부트 profile과 maven/gradle profile의 차이 설명 

- https://eblo.tistory.com/58

 

Spring boot - 빌드 profile 설정

스프링부트 profile과 Maven/Gradle의 profile의 차이 스프링부트의 profile은 runtime시 작동하는 것이고 Maven/Gradle의 profile은 build 시점 동작하는 것입니다. maven/gradle에서는 profile에 따라 해당 설정..

eblo.tistory.com

 

github : https://github.com/kkaok/study-springboot/tree/main/src/main/resources

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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 27
28 29 30
글 보관함