ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|---|
|
エラーの状況
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.example.UserService required a bean of type
'com.example.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.UserRepository' in your configuration.
Spring が UserService の userRepository フィールドに注入すべき Bean を見つけられなかった、というエラー。アプリ起動自体が失敗します。
主な原因と対処
原因 1: Bean としてアノテーションされていない
// ダメな例
public class UserRepository { // ← @Repository などなし
public User findById(Long id) { ... }
}
@Service
public class UserService {
@Autowired
private UserRepository userRepository; // ← Bean がない → 注入失敗
}
// 修正
@Repository
public class UserRepository { ... }
原因 2: @ComponentScan の範囲外
// メインクラス
package com.example;
@SpringBootApplication // デフォルトで com.example 以下をスキャン
public class MyApp { ... }
// com.example.service.UserService → OK
// com.example.repository.UserRepository → OK
// com.other.UserRepository → スキャン対象外!
// 修正: スキャン範囲を広げる
@SpringBootApplication(scanBasePackages = {"com.example", "com.other"})
// または
@ComponentScan(basePackages = {"com.example", "com.other"})
原因 3: インタフェースに対応する実装クラスがない
public interface UserRepository {
User findById(Long id);
}
@Service
public class UserService {
private final UserRepository userRepository; // インタフェースで型指定
// ← UserRepository を実装したクラスが Bean になっていない → エラー
}
// 修正案 1: 実装クラスを作って @Component
@Repository
public class UserRepositoryImpl implements UserRepository {
public User findById(Long id) { ... }
}
// 修正案 2: Spring Data JPA なら interface 自動実装
@Repository
public interface UserRepository extends JpaRepository {
// Spring が自動でプロキシ実装を作る
}
原因 4: @Configuration の @Bean メソッドが漏れている
@Configuration
public class AppConfig {
// この Bean が定義されていなければエラー
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
原因 5: @Profile で除外されている
@Service
@Profile("prod") // prod プロファイルのときだけ Bean になる
public class ProductionUserRepository implements UserRepository { ... }
# アクティブプロファイルが dev だと UserRepository の Bean がない
# 修正
# application.properties
spring.profiles.active=prod
# または開発用 Bean も用意
@Service
@Profile("dev")
public class DevUserRepository implements UserRepository { ... }
原因 6: starter / 依存ライブラリ未追加
// 例: spring-data-jpa を入れ忘れて Repository を作っている
// pom.xml に追加
org.springframework.boot
spring-boot-starter-data-jpa
// 例: DB ドライバの依存忘れ
com.h2database
h2
runtime
注入先と注入元の組み合わせ確認
| 注入先 (フィールド型) | 注入元の Bean 名 / 型 | 必要な前提 |
|---|---|---|
UserRepository | 同名 or 実装クラス | @Component / @Repository |
List | Notifier 型の全 Bean | 複数 @Component |
Map | Bean 名をキー | 同上 |
Optional | 0 個 or 1 個許容 | 同上 (空でも OK) |
デバッグ Tips
① 全 Bean をログ出力
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(MyApp.class, args);
String[] names = ctx.getBeanDefinitionNames();
Arrays.sort(names);
for (String name : names) System.out.println(name);
}
}
② actuator/beans エンドポイント
# application.properties
management.endpoints.web.exposure.include=beans
# 確認
$ curl http://localhost:8080/actuator/beans | jq .
③ デバッグログ
logging.level.org.springframework.beans=DEBUG
logging.level.org.springframework.context=DEBUG
同型 Bean が複数あるパターン (BeanNotOfRequiredTypeException / NoUniqueBean)
逆に「Bean がない」ではなく「複数あって絞り込めない」場合は NoUniqueBeanDefinitionException:
// 解決策
@Service
public class UserService {
// @Qualifier で名前指定
public UserService(@Qualifier("primaryNotifier") Notifier notifier) { ... }
// または @Primary で優先指定
}
@Primary
@Service
public class EmailNotifier implements Notifier { ... }
関連記事
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- java.lang.IllegalStateException: CGLIB is required to process @Configuration classes
- Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
- No mapping found for HTTP request with URI ... in DispatcherServlet with name ...
- An internal error occurred during: "Building UI model". com/google/common/base/Function
- No identifier specified for entity : ...
- org.hibernate.hql.internal.ast.QuerySyntaxException: table_name is not mapped
- No compiler is provided in this environment
- java.sql.SQLException: The server time zone value ' ... ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone
- Caused by: java.lang.RuntimeException: Executing an update/delete query
- Not supported for DML operations
- Field ... required a bean of type ... hat could not be found.
- Annotation-specified bean name ' ... ' for bean class [ ... ] conflicts with existing, non-compatible bean definition of same name and class [...]
- Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
- Exception in thread "main" java.lang.UnsupportedClassVersionError
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?