본문 바로가기

Engineering/Spring

spring boot 사용 예제

728x90

STS 에 언제부터인가 포함되어 있던 spring-boot 를 한번 테스트해보기로 했다.

Spring Boot 는 링크에서도 간단히 설명이 나와있지만, 어떤 xml 설정이 필요없고 독립형(stand-alone) 프로그램을 지원하고 웹 컨테이너를 내장해서(embbed) tomcat, jetty 같은 외부 WAS 없이도 웹앱을 실행할 수 있다.

"Package Explorer" - "NEW" - "Spring Starter Project" 를 선택하면 프로젝트 이름을 "demo" 로 해서 그룹, artifact, 패키지 이름, 그리고 style 이라고 해서 기본이 되는 모듈(Web, WebSocket, JDB, JPA, AMQP, ...)을 선택할 수 있다.


일단 "Web" 을 선택하면 maven 모듈들이 잔뜩 설치되고, 자동 생성된 Application 클래스를 확인해보면 main() 이 만들어진것을 확인할 수 있다.

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

간단히 "/" 를 라우트하는 코드를 추가하면

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Controller
public class Application {

	@RequestMapping("/")
	@ResponseBody
	String home() {
		return "Hello World!";
	}

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

STS 에서 "Run" - "main" 을 하면 다음과 같은 배너와 함께 8080 포트를 listening 한다는 메시지가 나온다.

  .   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::        (v1.1.4.RELEASE)


2014-07-09 16:09:56.140  INFO 7428 --- [           main] demo.Application                         : Starting Application on Jeon-ui-iMac.local with PID 7428 (/Users/firstboos/Documents/workspace-sts-3.5.1.RELEASE/demoWeb/target/classes started by firstboos in /Users/firstboos/Documents/workspace-sts-3.5.1.RELEASE/demoWeb)

2014-07-09 16:09:56.185  INFO 7428 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@49684e94: startup date [Wed Jul 09 16:09:56 KST 2014]; root of context hierarchy

2014-07-09 16:09:56.625  INFO 7428 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]

2014-07-09 16:09:57.376  INFO 7428 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080

2014-07-09 16:09:57.551  INFO 7428 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat


웹 브라우저에서 localhost:8080 으로 접근하면 "Hello, World!" 가 보인다.


command line 에서 실행하기 위해서는 mvn 으로 패키지 빌드후, 실행할 수 있다.

# mvn package

# mvn spring-boot:run


jar 로 패키지를 생성해서 실행할 수 있다.

# pwd

/Users/firstboos/Documents/workspace-sts-3.5.1.RELEASE/demo

# mvn package

# java -jar target/demo-0.0.1-SNAPSHOP.jar


  .   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::        (v1.1.4.RELEASE)


2014-07-09 16:30:22.432  INFO 7523 --- [           main] demo.Application                         : Starting Application on Jeon-ui-iMac.local with PID 7523 (/Users/firstboos/Documents/workspace-sts-3.5.1.RELEASE/demoWeb/target/demo-0.0.1-SNAPSHOT.jar started by firstboos in /Users/firstboos/Documents/workspace-sts-3.5.1.RELEASE/demoWeb)

2014-07-09 16:30:22.488  INFO 7523 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@294c4c55: startup date [Wed Jul 09 16:30:22 KST 2014]; root of context hierarchy

....


참고 :

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-installing-the-cli