1
0

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 3 years have passed since last update.

Spring Boot のServiceクラスを単独でHelloWorldする方法

Last updated at Posted at 2021-07-16

Spring Boot の HelloWorld のやり方をググってみたら、
ControllerクラスからHTMLファイルを呼び出す方法ばかりが検索結果に表示され、
Serviceクラスを単独で動かす方法が(私が見た限りで)無かったので、ここで共有します。

##環境
STS Version: 4.11.0.RELEASE
Java 11
Spring Boot 2.5.2

##準備
###プロジェクト作成
-「File」>「New」> 「Spring Starter Project」
-Nameは適当、Java Versionは「11」、Typeは「Gradle」を選択し「Next」を押下
-「Lombok」「Spring Web」「Thymeleaf」にチェックを入れ、「Finish」を押下

###Serviceクラスの作成

  1. パッケージの作成
    -com.example.demoパッケージを右クリック>「New」>「Package」
    -Nameに「com.example.demo.service」と入力し、「Finish」を押下

  2. Serviceクラスの作成
    -com.example.demo.serviceパッケージを右クリック>「New」>「Class」
    -Nameに「HelloServece」と入力し、「Finish」を押下

  3. Serviceクラスを以下のように修正

HelloService.java
package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
	
	public void hello(){
	System.out.println("Hello Service!!");
	}
}

###Applicationクラスの修正
Applicationクラスを以下のように修正

Demo2Application.java
package com.example.demo;

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

import com.example.demo.service.HelloService;

@SpringBootApplication
public class Demo2Application {
	
	public static void main(String[] args) {
		ConfigurableApplicationContext ctx = SpringApplication.run(Demo2Application.class, args);
		
		HelloService service = ctx.getBean(HelloService.class);
		
		service.hello();
		
	}

}

ConfigurableApplicationContext

でコンテナにApplicationクラスを登録し、

HelloService service = ctx.getBean(HelloService.class);

でServiceクラスをコンテナから取得しています。

##実行


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.5.2)[0;39m

[2m2021-07-16 23:38:39.879[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.example.demo.Demo2Application       [0;39m [2m:[0;39m Starting Demo2Application using Java 16.0.1 on PBPC0185 with PID 141252 (C:\Users\pbhosoda\Documents\workspace-spring-tool-suite-4-4.11.0.RELEASE\demo-2\bin\main started by pbhosoda in C:\Users\pbhosoda\Documents\workspace-spring-tool-suite-4-4.11.0.RELEASE\demo-2)
[2m2021-07-16 23:38:39.886[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.example.demo.Demo2Application       [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2021-07-16 23:38:41.052[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2021-07-16 23:38:41.063[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2021-07-16 23:38:41.064[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.48]
[2m2021-07-16 23:38:41.205[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2021-07-16 23:38:41.205[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1244 ms
[2m2021-07-16 23:38:41.778[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2021-07-16 23:38:41.793[0;39m [32m INFO[0;39m [35m141252[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.example.demo.Demo2Application       [0;39m [2m:[0;39m Started Demo2Application in 2.406 seconds (JVM running for 3.555)
Hello Service!!

いけてますね。

Applicationクラスに@Componentをつけて、@Autowiredでserviceクラスを取得すれば
いいんじゃないかと思ったのですが、それだとエラーが出ました。

原因が分かったら、また投稿しようと思います。
終わり。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?