※初めて質問します。よろしくお願いいたします。
実現したいこと
現在、Spring Bootのマルチプロジェクトを作成しており、参照元のプロジェクトの中で
参照先の別モジュールにある依存クラスを@Autowiredしたいです。
現在、発生している問題(概要)
@ComponentScanにより参照先プロジェクトにある依存クラスを@Autowiredしようとした際に
参照元クラスのbeanも生成されなくなります。
具体的には@RestControllerアノテーションをつけたContorollerです。
前提
フォルダ構成は以下のパッケージエクスプローラーの通りです。
sample-appがcommon-businessを参照しているという関係性です。
現在、発生している問題(詳細)
sample-app/build.gradleにcommon-businessへの参照設定を追記してマルチプロジェクトとし、
sample-appのmainクラスにて@ComponentScanを付与したところsample-appのBean自体が生成されなくなりました。
具体的には以下のControllerで@ComponentScanをはずすとBeanがうまく生成され、localhost:8080/sample/api/testへのアクセスも成功します。
java
1package spring.demo.controller; 2import spring.common.demo.ItemService; 3@RestController 4public class ItemController { 5 @Autowired 6 ItemService itemService; 7 8 @GetMapping("/sample/api/test") 9 public String test() { 10 return "Hello World"; 11 } 12}
sample-appからcommon-businessを参照するビルドスクリプトが悪いのか、
別モジュールにある依存クラスへの参照設定が悪いのか
それともその他の部分に問題があるのかわかりません。
お手数をおかけしますが、原因部分とその対応策のご教示をお願い致します。
該当のソースコード
sample-appのbuild.gradle及びsetting.gradleは以下の通りです。
/sample-app/build.gradle
gradle
1buildscript { 2 ext { 3 springBootVersion = '2.1.3.RELEASE' 4 } 5} 6plugins { 7 id 'org.springframework.boot' version '2.1.3.RELEASE' 8 id 'java' 9} 10apply plugin: 'io.spring.dependency-management' 11group = 'spring.demp' 12version = '0.0.1-SNAPSHOT' 13sourceCompatibility = '1.8' 14 15repositories { 16 mavenCentral() 17} 18 19subprojects { 20 apply plugin: "java" 21 apply plugin: "io.spring.dependency-management" 22 23 repositories { 24 mavenCentral() 25 } 26 dependencyManagement { 27 imports { 28 mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion" 29 } 30 } 31} 32 33dependencies { 34 compile project(':common-business') 35 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 36 implementation 'org.springframework.boot:spring-boot-starter-jdbc' 37 implementation 'org.springframework.boot:spring-boot-starter-web' 38 compileOnly 'org.projectlombok:lombok' 39 runtimeOnly 'org.postgresql:postgresql' 40 testImplementation 'org.springframework.boot:spring-boot-starter-test' 41} 42
/sample-app/setting.gradle
gradle
1 2rootProject.name = 'sample-app' 3 4include ':common-business' 5project(':common-business').projectDir = new File('../common-business')
/common-business/build.gradle
gradle
1 2plugins { 3 id 'java' 4} 5 6repositories { 7 mavenCentral() 8} 9 10dependencies { 11 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 12 implementation 'org.springframework.boot:spring-boot-starter-jdbc' 13 implementation 'org.springframework.boot:spring-boot-starter-web' 14 compileOnly 'org.projectlombok:lombok' 15 runtimeOnly 'org.postgresql:postgresql' 16 testImplementation 'org.springframework.boot:spring-boot-starter-test' 17 18 // Use JUnit test framework 19 testImplementation 'junit:junit:4.12' 20} 21
試したこと
別モジュールにある依存クラスを@Autowiredするために@ComponentScan("common-business")としています。
補足情報(FW/ツールのバージョンなど)
Spring Boot 2.1.3
Gradle 4.10.2

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/05 01:54