ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring-1] Spring Boot Sample 따라 하기
    설치&설정 관련/Spring Framework 2014. 10. 14. 17:19
    728x90

    본 포스팅은 Spring 에서 제공되는 Building an Application with Spring Boot를 따라한 내용입니다.

    GIT 주소 : https://github.com/lahuman/SpringBoot.Sample


    Spring Boot 특징


    Spring Boot는 빠를 개발을 지원 합니다.

    사용자가 classpath나 bean 설정 등의 구조보다 비지니스 기능에 더 집중 하도록 합니다.


    Spring Boot를 이용하여 간단한 웹 어플리케이션을 제작 합니다.


    준비 사항

    • JDK 1.6 이상
    • Gradle 1.11 이상
    • IntelliJ 


    Intellij 를 이용하여 프로젝트 생성 및 테스트 하기


    1. GRADLE 프로젝트 생성 하기

    • auto inpormt와 기본 구조의 디렉토리를 생성 하는 두개의 체크 박스를 클릭 한다.

    • 프로젝트 명을 지정 하고 생성 한다.

    • 등록된 프로젝트는 위와 같은 구조를 가진다.

    apply plugin: 'java'
    version = '1.0'
    repositories {
        mavenCentral()
    }
    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web:1.1.8.RELEASE")
    }
    
    • build.gradle 내역 중 dependencies에  compile("org.springframework.boot:spring-boot-starter-web:1.1.8.RELEASE") 내역 추가


    • TIP : Gradle 설정 이후, 바로 반영이 안될 경우 Gradle tasks에서 새로 고침 버튼을 클릭 하면 바로 반영이 된다.

    2. class 생성
    @RestController
    public class SampleController {
        @RequestMapping("/")
        public String index() {
            return "Greeting from Sping Boot";
        }
    }
    
    • Controller 생성을 한다.
    • @RestController 태그의 의미는 Spring MVC 을 이용해 web의 요청을 기다린다는 의미이며, @RequestMapping 은 / URL을 index() 메소드에 연결하여 웹 브라우져에서 요청시 순수한 텍스트를 전달 한다.
    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application {
        public static void main(String[] args) {
            ApplicationContext ctx = SpringApplication.run(Application.class, args);
            System.out.println("Let's inspct the bens provider by Spring Boot:");
    
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        }
    }
    
    • Application Runner 생성을 한다.
    • @Configuration   현재 클래스가 설정 파일임을 Spring Context에게 알려준다.
    • @EnableAutoConfiguration 는 Spring Boot에서 기본 beans 들을 classpath 설정이나 다른 beans, 여러가지 property 설정들을 시작시 추가해 준다.
    • @ComponentScan 의 의미는 스프링에서 다른 components, configurations 과 service 를 해당 패키지(kr.pe.lahuman.springboot) 에서 찾게 한다.
    • 보통의 Spring MVC 에서 @EnableWebMvc 를 추가 해야 하지만, Spring Boot에서는 spring-webmvc 가 classpaht에 추가 될때 자동으로 추가 된다.

    3. 실행

    • Application Class 를 실행 하면 Console 에서 8080 포트를 이용 하여 구동 되는 것을 확인 할수 있다.
    • Spring Boot 는 기본적으로 Tomcat을 이용한다. 만약, Jetty를 이용하고 싶을 경우 build.gradle에 다음과 같이 설정 하여야 한다.
    dependencies {
        // tag::jetty[]
        compile("org.springframework.boot:spring-boot-starter-web") {
            exclude module: "spring-boot-starter-tomcat"
        }
        compile("org.springframework.boot:spring-boot-starter-jetty")
        // end::jetty[]
        testCompile("junit:junit")
    }
    
    • 실행 결과





    글을 쓰고 보니 더 좋은 포스트가 있네요.

    http://theeye.pe.kr/archives/2014 이 포스트 보다 더 잘 정리 되어 있습니다. 

    OTL


    728x90
Designed by Tistory.