12
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

春だからSpringbootを始めよう

Last updated at Posted at 2016-04-22

はじめに

Springは大変素晴らしいフレームワークなのは皆さんも御存知の通りですが
各種設定が複雑でよく理解していないまま、どこぞのサンプルの設定をコピペして使っているという状況も多いのでは?

Springbootは基本的にxmlでの設定レスで各種springのフレームワークを統合してくれているプロダクトです。
しかもTomcatを内蔵しているのでデプロイも不要です。

せっかく春ですし、この機会にちょっと触ってみようかと思ったのでまとめてみます。
使った環境はいつものようにEclipse4.5とMavenです。

SPRING INITIALIZR

例によってmavenなりgradleなりに依存関係を書いていくわけですが、
SPRING INITIALIZR
というサイトがあります。
必要そうなプロダクトを入力するだけでプロジェクトの雛形を作ってくれるありがたいサイトなので使っていきましょう。

springInitializr.PNG 画面はWebとテンプレートエンジンとしてThymeleafを選択してる状態

必要なプロダクトを選択したらGenerate Projectでzipを落としましょう。

インポート

SPRING INITIALIZRで作成したzipを解凍します。
解凍したファイルをEclipseのワークスペースにコピーなり移動なりしておきます。
Eclipseでファイル>インポートから既存Mavenプロジェクトを選択します。
mavenproject.PNG

参照からワークスペース配下においた場所を選択し完了を押します。
maven2.PNG

これでインポート完了です。

実装

とりあえずいつものようにHelleWorldします。

HelloController.java
package net.zantetu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

	@RequestMapping("/")
	public String hello(Model model){
		model.addAttribute("hello", "hello");
		return "hello";
	}
}
hello.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
	<span th:text="${hello + ' world'}"></span>
</body>
</html>

構成
kousei.PNG

SpringboottestApplication.javaがMainクラスになります。
これを普通に実行すればTomcatも立ち上がります。

起動ログ

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.3.RELEASE)

2016-04-22 11:00:44.142  INFO 13452 --- [           main] net.zantetu.SpringboottestApplication    : Starting SpringboottestApplication on RT0035 with PID 13452 (C:\pleiades-e4.5-ultimate-jre_20160229\pleiades\workspace\springboottest\target\classes started by aono in C:\pleiades-e4.5-ultimate-jre_20160229\pleiades\workspace\springboottest)
2016-04-22 11:00:44.145  INFO 13452 --- [           main] net.zantetu.SpringboottestApplication    : No active profile set, falling back to default profiles: default
2016-04-22 11:00:44.581  INFO 13452 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52525845: startup date [Fri Apr 22 11:00:44 JST 2016]; root of context hierarchy
2016-04-22 11:00:45.435  INFO 13452 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: 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]]
2016-04-22 11:00:46.466  INFO 13452 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-04-22 11:00:46.486  INFO 13452 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-04-22 11:00:46.488  INFO 13452 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.32
2016-04-22 11:00:46.627  INFO 13452 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-04-22 11:00:46.627  INFO 13452 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2048 ms
2016-04-22 11:00:47.025  INFO 13452 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-04-22 11:00:47.031  INFO 13452 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-04-22 11:00:47.032  INFO 13452 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-04-22 11:00:47.032  INFO 13452 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-04-22 11:00:47.032  INFO 13452 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-04-22 11:00:47.428  INFO 13452 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52525845: startup date [Fri Apr 22 11:00:44 JST 2016]; root of context hierarchy
2016-04-22 11:00:47.527  INFO 13452 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String net.zantetu.controller.HelloController.hello(org.springframework.ui.Model)
2016-04-22 11:00:47.535  INFO 13452 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-04-22 11:00:47.536  INFO 13452 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-04-22 11:00:47.587  INFO 13452 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-04-22 11:00:47.588  INFO 13452 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-04-22 11:00:47.657  INFO 13452 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-04-22 11:00:48.370  INFO 13452 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-04-22 11:00:48.467  INFO 13452 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-22 11:00:48.476  INFO 13452 --- [           main] net.zantetu.SpringboottestApplication    : Started SpringboottestApplication in 4.787 seconds (JVM running for 5.327)
2016-04-22 11:01:02.788  INFO 13452 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-04-22 11:01:02.788  INFO 13452 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-04-22 11:01:02.806  INFO 13452 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms

画面
hello.PNG
うまく動いたっぽいですね。

まとめ

Springboot自体は基本はとても簡単に実装できることがわかりました。
これから他のSpring系プロダクトとの連携とかも試していきたいです。

12
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?