woniper

spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용) 본문

Spring

spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용)

woniper1 2014. 10. 25. 14:58

[spring] spring boot-1(특징과 기본 설정)
[spring] spring boot-3(JPA 설정과 사용)
[spring] spring boot-4(Velocity 설정과 사용)


Spring Boot 프로젝트 구조


src > main > java : java class

src > main > resources : view Template(templates 폴더), resource(static 폴더), properties 구현

src > test > java : test 코드 java class


  처음 spring프로젝트 구조를 볼때가 생각난다. 지금은 어느정도 이해를 하고 있는 상태라 그런지 Spring Boot에 프로젝트 구조가 일반 프로젝트 구조보다 간단하고 느껴진다. 물론 프로젝트 마다 구조는 다 틀리겠지만 말이다.



Tomcat 연동

1. pom.xml

  외부 tomcat을 사용하기 위해 package를 war로 변경한다.



    4.0.0

    net.woniper
    example
    1.0-SNAPSHOT
    war

    
        org.springframework.boot
        spring-boot-starter-parent
        1.1.8.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        net.woniper.springboot.Application
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2. SpringBootServletInitializer 구현

  Spring Boot는 jar파일로 run이 되기 때문에 외부 톰캣을 사용해서 run하기 위해서는 war파일로 변경 후 SpringBootServletInitializer를 사용해 톰캣과 연결한다. 여기서 톰캣 연결하는 방법은 생략하겠다. 너무 간단하기 때문에..

package net.woniper.springboot;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * Created by woniper on 14. 10. 25..
 */
public class AppInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}


properties 사용

  Spring Boot에서는 application.properties파일을 여러가지 설정이 가능하다. 다 설명하기는 어렵지만 잘정리된 Spring Boot Properties 참고 문서를 참고하면 쉽게 설정이 가능하다. 이 포스팅에서는 properties를 사용해 profile설정을 해볼 것이다.


1. application.properties 추가



  Spring Boot Properties 참고 문서를 참고해서 application.properties파일에 설정하면된다. Spring Boot를 쓰면서 그리고 포스팅하면서 계속 느끼는거지만 정말 편하다. 그리고 쉽다.


2. profile 설정

  profile은 한 프로젝트가 여러 환경에서 사용가능하게 설정해주는 것인데 쉽게 말해서 lcoal환경과 test환경을 나눠서 설정 할 수있다. 예를 들면 local에서 jdbc url이 localhost:3306이라면 test환경에 jdbc url은 test.jdbc.com:3306일 수 있기 때문이다. test서버에 배포 할때마다 jdbc url을 test환경에 맞게 변경후 배포를 할 필요가 없다는 것이다. profile설정이 없다면 실수로 local 환경으로 test환경에 배포 되어 오류가 날 수도있는 것이다.

@Profile 어노테이션을 사용해서 간단하게 사용가능하다.


1. Spring Boot에서 Profile 설정

  application-{profile_name}.properties로 설정이 가능하다. "{profile_name}"이 profile명이 들어가면 그 properties파일이 profile만에 properties가 되는 것이다. 물론 application.properties는 profile과 상관없이 공통으로 사용된다.

local 환경과 test환경으로 예를 들겠다.

- local 환경

  properties명 : application-local.properties


- test 환경

  properties명 : application-test.properties



  자 그럼 local 환경에서는 localhost:3306이라는 jdbc url을 사용하고, test 환경에서는 test.jdbc.com:3306이라는 jdbc url을 사용하게 되는 것이다.


2. tomcat profile 설정

{tomcat-root}/bin/setenv.sh 파일 생성 후 아래 입력

export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active={profile_name}"


'Spring' 카테고리의 다른 글

spring boot-4(Velocity 설정과 사용)  (0) 2014.10.25
spring boot-3(JPA 설정 및 사용)  (12) 2014.10.25
spring boot-1(특징과 기본 설정)  (0) 2014.10.25
HATEOAS 사용하기  (1) 2014.07.27
이메일 전송  (0) 2014.07.13
Comments