質問編集履歴

5

タイトルを修正

2019/03/01 02:51

投稿

4321bocya
4321bocya

スコア13

test CHANGED
@@ -1 +1 @@
1
- Spring Bootのマルチプロジェクトで別モュールにある依存クラスを@Autowiredでき
1
+ Spring Bootのマルチプロジェクトで@ComponentScanにより参照先プロェクトにある依存クラスを@Autowiredしようとすると 参照元クラスのbeanも生成されくなる
test CHANGED
File without changes

4

内容を修正

2019/03/01 02:51

投稿

4321bocya
4321bocya

スコア13

test CHANGED
@@ -1 +1 @@
1
- Spring Bootのマルチプロジェクトで別モジュールにある依存クラスを@AutowiredするとAPI呼び出しが404と
1
+ Spring Bootのマルチプロジェクトで別モジュールにある依存クラスを@Autowiredでき
test CHANGED
@@ -1,5 +1,7 @@
1
1
  ※初めて質問します。よろしくお願いいたします。
2
2
 
3
+
4
+
3
5
  ### 実現したいこと
4
6
 
5
7
 
@@ -12,9 +14,11 @@
12
14
 
13
15
  ### 現在、発生している問題(概要)
14
16
 
15
- @ComponentScanにより別モュールにある依存クラスを@Autowiredしようとした際に
17
+ @ComponentScanにより参照先プロェクトにある依存クラスを@Autowiredしようとした際に
16
-
18
+
17
- 既存のコントローーへAPI呼び出しが404と
19
+ 参照元クbeanも生成されくなります
20
+
21
+ 具体的には@RestControllerアノテーションをつけたContorollerです。
18
22
 
19
23
 
20
24
 
@@ -30,7 +34,17 @@
30
34
 
31
35
 
32
36
 
37
+
38
+
39
+ ### 現在、発生している問題(詳細)
40
+
41
+
42
+
43
+ sample-app/build.gradleにcommon-businessへの参照設定を追記してマルチプロジェクトとし、
44
+
45
+ sample-appのmainクラスにて@ComponentScanを付与したところsample-appのBean自体が生成されなくなりました。
46
+
33
- そして、sample-appに以下のよなコントローラーを作しており参照設定をしていない状態であればlocalhost:8080/sample/api/testへのアクセス成功します。(Hello Worldが返ってくる)
47
+ 具体的には以下のControllerで@ComponentScanをはずすとBeanがまく生され、localhost:8080/sample/api/testへのアクセス成功します。
34
48
 
35
49
 
36
50
 
@@ -38,10 +52,18 @@
38
52
 
39
53
  package spring.demo.controller;
40
54
 
55
+ import spring.common.demo.ItemService;
56
+
41
57
  @RestController
42
58
 
43
59
  public class ItemController {
44
60
 
61
+ @Autowired
62
+
63
+ ItemService itemService;
64
+
65
+
66
+
45
67
  @GetMapping("/sample/api/test")
46
68
 
47
69
  public String test() {
@@ -54,48 +76,6 @@
54
76
 
55
77
  ```
56
78
 
57
- ### 現在、発生している問題(詳細)
58
-
59
-
60
-
61
- sample-app/build.gradleにcommon-businessへの参照設定を追記してマルチプロジェクトとし、
62
-
63
- web-appにてcommon-businessのItemServiceを@Autowiredすると
64
-
65
- localhost:8080/sample/api/testへのアクセスが404となってしまいます。
66
-
67
-
68
-
69
- ```java
70
-
71
- package spring.demo.controller;
72
-
73
- import spring.common.demo.ItemService;
74
-
75
- @RestController
76
-
77
- public class ItemController {
78
-
79
- //追記部分
80
-
81
- @Autowired
82
-
83
- ItemService itemService;
84
-
85
-
86
-
87
- @GetMapping("/sample/api/test")
88
-
89
- public String test() {
90
-
91
- return "Hello World";
92
-
93
- }
94
-
95
- }
96
-
97
- ```
98
-
99
79
 
100
80
 
101
81
 
@@ -114,28 +94,6 @@
114
94
 
115
95
 
116
96
 
117
- ### 発生している問題・エラーメッセージ
118
-
119
- common-businessへの参照をした後に再度localhost:8080/sample/api/testへのアクセスした際の標準出力は以下の通りです。
120
-
121
- 特にエラーは出ていません。
122
-
123
-
124
-
125
- ```
126
-
127
- 2019-03-01 00:13:34.844 INFO 8432 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
128
-
129
- 2019-03-01 00:13:34.845 INFO 8432 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
130
-
131
- 2019-03-01 00:13:34.862 INFO 8432 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms
132
-
133
-
134
-
135
- ```
136
-
137
-
138
-
139
97
  ### 該当のソースコード
140
98
 
141
99
 

3

内容の修正

2019/03/01 02:50

投稿

4321bocya
4321bocya

スコア13

test CHANGED
File without changes
test CHANGED
@@ -116,7 +116,9 @@
116
116
 
117
117
  ### 発生している問題・エラーメッセージ
118
118
 
119
- localhost:8080/sample/api/testへのアクセスした際の標準出力は以下の通りです。
119
+ common-businessへの参照をした後に再度localhost:8080/sample/api/testへのアクセスした際の標準出力は以下の通りです。
120
+
121
+ 特にエラーは出ていません。
120
122
 
121
123
 
122
124
 
@@ -140,6 +142,8 @@
140
142
 
141
143
  sample-appのbuild.gradle及びsetting.gradleは以下の通りです。
142
144
 
145
+ /sample-app/build.gradle
146
+
143
147
 
144
148
 
145
149
  ```gradle
@@ -232,9 +236,9 @@
232
236
 
233
237
 
234
238
 
239
+ /sample-app/setting.gradle
240
+
235
- build.gradle
241
+ ```gradle
236
-
237
- ```
238
242
 
239
243
 
240
244
 
@@ -248,6 +252,58 @@
248
252
 
249
253
  ```
250
254
 
255
+
256
+
257
+ /common-business/build.gradle
258
+
259
+ ```gradle
260
+
261
+
262
+
263
+ plugins {
264
+
265
+ id 'java'
266
+
267
+ }
268
+
269
+
270
+
271
+ repositories {
272
+
273
+ mavenCentral()
274
+
275
+ }
276
+
277
+
278
+
279
+ dependencies {
280
+
281
+ implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
282
+
283
+ implementation 'org.springframework.boot:spring-boot-starter-jdbc'
284
+
285
+ implementation 'org.springframework.boot:spring-boot-starter-web'
286
+
287
+ compileOnly 'org.projectlombok:lombok'
288
+
289
+ runtimeOnly 'org.postgresql:postgresql'
290
+
291
+ testImplementation 'org.springframework.boot:spring-boot-starter-test'
292
+
293
+
294
+
295
+ // Use JUnit test framework
296
+
297
+ testImplementation 'junit:junit:4.12'
298
+
299
+ }
300
+
301
+
302
+
303
+ ```
304
+
305
+
306
+
251
307
  # 試したこと
252
308
 
253
309
  別モジュールにある依存クラスを@Autowiredするために@ComponentScan("common-business")としています。

2

タイトルを修正

2019/02/28 23:17

投稿

4321bocya
4321bocya

スコア13

test CHANGED
@@ -1 +1 @@
1
- Spring BootにAPI呼び出しをした際に404となる
1
+ Spring Bootのマルチプロジェクトで別モジュールある依存クラスを@AutowiredするとAPI呼び出し404となる
test CHANGED
File without changes

1

Spring Bootのバージョンの記載を修正

2019/02/28 15:56

投稿

4321bocya
4321bocya

スコア13

test CHANGED
File without changes
test CHANGED
@@ -256,6 +256,6 @@
256
256
 
257
257
  ### 補足情報(FW/ツールのバージョンなど)
258
258
 
259
- Spring Boot 2.1.0
259
+ Spring Boot 2.1.3
260
260
 
261
261
  Gradle 4.10.2