トッカンソフトウェア

SpringBootインストール(sts3)

今回からSpringBootをやってみます。まずはインストールから。



SpringBootのダウンロード

EclipseやVisual Studio Codeのプラグインがあるみたいですが、STS(Spring tool suite)という専用のIDEを使ってみます。
ダウンロードはhttps://spring.io/tools3/sts/allからできます。



zipがダウンロードできるので、展開します。
(現時点のバージョンは、spring-tool-suite-3.9.8.RELEASE-e4.11.0-win32-x86_64.zip でした)

STSの実行とプロジェクト作成


展開先の中からSTS.exeを実行します。
sts-bundle\sts-3.9.8.RELEASE\STS.exe

起動時にワークスペースの場所を聞かれますが、適当に指定します。

起動したらSpringBootプロジェクトを作ってみます。
メニュー → File → New → Spring Starter Project を選択します。


適当にプロジェクト情報を入力します。今回はデフォルトのままにしてみました。


使用するパッケージ等を指定します。WebとThymeleafを指定して下さい。
今回は単純なHello Worldを表示するだけのサンプルですが、、Thymeleafも指定して下さい。


ここはこのままでFinishボタンを押します。


上記の作業でプロジェクトが作成され、以下のソースが自動で作成されます。

DemoApplication.java
				
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}


			

pom.xml
				
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>


			
そのまま実行しても実行できますが、


該当ページ(http://localhost:8080/)を開いても、ページがないようなエラー画面が表示されます。


画面表示

画面を表示させるため、SpringMVCと同じようにコントローラとHTMLを追加します。

TestController.java
				
package com.example.demo;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

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

@Controller
public class TestController {

	@RequestMapping(value = "/", method = GET)
	public String show() {
		return "test";
	}
}

			
test.html
				
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>Hello World
</body>
</html>

			
ファイル構成は以下のようになります。


再度実行し、該当ページ(http://localhost:8080/)にアクセスするとWEB画面が表示されます。


注意点

■2重起動について
WEBサーバを起動するときは、普通にJavaからmainメソッドを実行するのですが、2重で実行できてしまい、以下のようなエラーが出ます。
				

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

2019-05-19 09:01:14.606  INFO 972 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on PC with PID 972 (C:\sts-bundle\workspace-sts-3.9.8.RELEASE\demo\target\classes started by akira in C:\sts-bundle\workspace-sts-3.9.8.RELEASE\demo)
2019-05-19 09:01:14.610  INFO 972 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2019-05-19 09:01:15.680  INFO 972 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-05-19 09:01:15.706  INFO 972 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-19 09:01:15.706  INFO 972 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-05-19 09:01:15.822  INFO 972 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-19 09:01:15.822  INFO 972 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1150 ms
2019-05-19 09:01:16.054  INFO 972 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-19 09:01:16.395 ERROR 972 --- [           main] org.apache.catalina.util.LifecycleBase   : Failed to start component [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Protocol handler start failed
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:1008) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.catalina.core.StandardService.addConnector(StandardService.java:226) [tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:259) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:197) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:311) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:164) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) [spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]
Caused by: java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_73]
	at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_73]
	at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_73]
	at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ~[na:1.8.0_73]
	at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ~[na:1.8.0_73]
	at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:239) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:213) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1116) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1202) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:568) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:1005) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
	... 14 common frames omitted

2019-05-19 09:01:16.403  INFO 972 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-05-19 09:01:16.446  INFO 972 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-19 09:01:16.451 ERROR 972 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.

2019-05-19 09:01:16.453  INFO 972 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8


			
■コントローラの置き場所について
@ComponentScan(component-scan)を設定しない場合、Springのアノテーションが読み込まれるのはmainメソッドを実行したパッケージかその配下パッケージになります。
別の場所にコントローラをおいても読み込まれません。

ページのトップへ戻る