質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

2回答

2758閲覧

Spring Bootのマルチモジュール構成で、別モジュールにある依存クラスが@Autowiredできない

AIUeno

総合スコア15

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2018/01/08 03:26

親パッケージ、子モジュール☓2の構成でプロジェクトを作っています。

ディレクトリ構成は以下になります。

Package ←親プロジェクト
├── admin ←子モジュール
│   ├── pom.xml
│   └── src
│      └── main
│         └── java
│            └── admin
│               ├── AdminApplication.java
│               ├── WebSecurityConfig.java
│               ├── app
│               │   └── MainController.java
│               ├── component
│               └── domain
│         ├── model
│         ├── repository
│         └── service
├── web ←子モジュール
│ ├── pom.xml
│   ├── src
│      ├── main
│         ├── java
│            └── web
│            ├── WebApplication.java
│            ├── WebSecurityConfig.java
│            ├── app
│            │ └── WebMainController.java
│         └── service

※親パッケージを作ったのは、2つのプロジェクトで
DBが共通のため、子モジュールのwebでは、admin.domain以下を使いたかったためです。

Webモジュールの
WebApplication.java

Java

1package web; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication(scanBasePackages = {"web", "admin.domain"}) 7public class WebApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(WebApplication.class, args); 11 12 } 13 14}

eclipseでwebモジュールを起動しようとすると、
以下エラーが出て起動できませんでした。

txt

1org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'admin.domain.repository.customer.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 3 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 4 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 5 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 6 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 7 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 8 9.... 略 10 11 2018-01-08 12:19:26.610 ERROR 17257 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : 12 13 *************************** 14 APPLICATION FAILED TO START 15 *************************** 16 17 Description: 18 19 Field customerRepository in web.domain.service.customer.AuthCustomerDetailsService required a bean of type 'admin.domain.repository.customer.CustomerRepository' that could not be found. 20 21 22 Action: 23 24 Consider defining a bean of type 'admin.domain.repository.customer.CustomerRepository' in your configuration. 25

子モジュールWebのAuthCustomerDetailsService.java

Java

1 package web.domain.service.customer; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.security.core.userdetails.UserDetails; 5 import org.springframework.security.core.userdetails.UserDetailsService; 6 import org.springframework.security.core.userdetails.UsernameNotFoundException; 7 import org.springframework.stereotype.Component; 8 9 import admin.domain.model.PtCustomer; 10 import admin.domain.repository.customer.CustomerRepository; 11 12 13 @Component 14 public class AuthCustomerDetailsService implements UserDetailsService { 15 16 @Autowired 17 CustomerRepository customerRepository; 18 19 @Override 20 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 22 PtCustomer customer = customerRepository.findByCode(username); 23 if(customer == null){ 24 throw new UsernameNotFoundException(username + " is not found."); 25 } 26 27 return new AuthCustomerDetails(customer); 28 } 29 30 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

WEB,ADMIN,PACKAGEの関係的に

PACKAGE には module で WEB,ADMIN を定義し
WEB には parentPACKAGE を定義し dependencyADMIN を定義し
ADMIN には parentPACKAGE を定義し

すべてにおいてmvn インストールがすんでいないからかもね

投稿2018/01/08 11:35

編集2018/01/08 11:37
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

AIUeno

2018/01/09 00:31

回答ありがとうございます! module と parentの設定はしていましたが、dependencyの設定はしていませんでした。 pom.xmlへの記述の仕方がわかりません。
AIUeno

2018/01/09 00:37

pom.xmlの修正、および全てのモジュールでmvnインストールを実行してみましたが、 状況は変わりませんでした。
guest

0

自己解決

mainのクラスに@componentscan を記述することで、上記のエラーは発生しなくなりました。

WebApplication.java

Java

1package web; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7@ComponentScan(basePackages = {"web", "admin"}) 8public class WebApplication { 9 10 public static void main(String[] args) { 11 SpringApplication.run(WebApplication.class, args); 12 13 } 14

@ComponentScan(basePackages = {"web", "admin.domain"})
として、DBがらみの必要なところだけ読み込みたかったのですが、これだとエラーが解消せず。
"admin"で丸ごと指定するとエラーがでなくなりました。

しかし、この状態だと、管理側ログインとWeb側ログインのWebSecurityConfigがバッティングして
意図した動作になりません。

excludeFilters=でadmin側のWebSecurityConfigを読み込まないよう除外したいのですが、
また別の現象なので、新しく質問を起こすことにします。
ありがとうございました。

投稿2018/01/09 01:20

AIUeno

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/01/09 03:27

Spring Boot で複数のログイン画面を使う http://d.hatena.ne.jp/hagi44/20151210/1449750076 でどうにかなりそうなきもしないでもないが WEB,ADMIN,ADMIN-DOMAIN の3分割にしたらいいきがするが・・・(もしくは Package に ドメインを移植)
退会済みユーザー

退会済みユーザー

2018/01/09 03:28

ほかの JAR の中を検索するときは componentscan は必須
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問