| この記事の要点 |
|
このエラーの概要
Spring アプリ起動時に次のスタックトレースで失敗します:
Caused by: java.lang.IllegalStateException: CGLIB is required to process
@Configuration classes. Either add CGLIB to the classpath or remove the
following @Configuration bean definitions:
[appConfig]
at org.springframework.context.annotation.ConfigurationClassPostProcessor
.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:434)
これは Spring が @Configuration クラスを「実体クラスのまま」ではなく CGLIB 動的サブクラスとして動かそうとしたとき、CGLIB が classpath に居ないと発生します。
なぜ CGLIB が必要か
Spring は @Configuration クラスの @Bean メソッド間呼び出し(inter-bean reference)を制御するために、クラスを CGLIB でサブクラス化してプロキシ化します:
@Configuration
public class AppConfig {
@Bean
public UserRepository userRepository() {
return new UserRepository();
}
@Bean
public UserService userService() {
// ↓ この呼び出しが「都度 new UserRepository()」ではなく、
// Spring コンテナから既存のシングルトンを取得するように
// CGLIB プロキシによって書き換えられる
return new UserService(userRepository());
}
}
CGLIB が無いと、userRepository() 呼び出しが本当に都度 new されてしまい、シングルトンが崩れます。Spring はそれを許容せず起動時に止めます。
対処1: CGLIB 依存を追加(Spring 4 系 / 依存除外している場合)
<!-- Maven (pom.xml) -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<!-- 依存解決問題を避けたいなら nodep 版(ASM を内包) -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.3.0</version>
</dependency>// Gradle (build.gradle)
dependencies {
implementation 'cglib:cglib:3.3.0'
// または nodep 版
implementation 'cglib:cglib-nodep:3.3.0'
}
対処2: proxyBeanMethods = false で CGLIB 不要に(Spring 5.2+)
inter-bean reference を使わないなら、CGLIB プロキシを完全に無効化できます:
// Spring 5.2 以降
@Configuration(proxyBeanMethods = false)
public class AppConfig {
@Bean
public UserRepository userRepository() {
return new UserRepository();
}
@Bean
public UserService userService(UserRepository repo) {
// ↑ メソッド呼び出しではなく、引数注入で受け取る
return new UserService(repo);
}
}
これで CGLIB は不要、起動も高速化。GraalVM Native Image でも有利。Spring Boot 2.2+ の @SpringBootConfiguration も内部で proxyBeanMethods = false 推奨パターンを採用しています。
| 設定 | 動作 | CGLIB | 起動時間 |
|---|---|---|---|
@Configuration(既定) | フルプロキシ、メソッド呼び出しを intercept | 必要 | 普通 |
@Configuration(proxyBeanMethods = false) | 軽量、メソッド呼び出しは普通の new | 不要 | 速い |
@Component | 同じく軽量、ただし @Bean メソッドは推奨されない | 不要 | 速い |
対処3: 依存除外を解除
誰かが exclusions で CGLIB / spring-core を除外している可能性があります:
<!-- ❌ こういう exclusions を消す -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
</exclusions>
</dependency>
依存ツリーを調べる:
# Maven
mvn dependency:tree | grep -i cglib
# Gradle
./gradlew dependencies --configuration runtimeClasspath | grep -i cglib
# 結果に cglib が出てこなければ classpath に無い
対処4: ASM ライブラリの確認
CGLIB は ASM に依存します。cglib 通常版を使うときは ASM のバージョン衝突に注意:
<!-- 衝突しがちな組み合わせ -->
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.5</version>
</dependency>
<!-- cglib-nodep は ASM を内包するので衝突しない(推奨) -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.3.0</version>
</dependency>
Spring Boot の場合
Spring Boot 2.x+ では spring-boot-starter が spring-core(内部に spring-asm + CGLIB 同等機能)を持っており、通常はこのエラーが出ません。出る場合は:
- 誰かが
spring-coreを除外している - Java バージョンが古い(Java 8 未満で動かそうとした)
- クラスローダー分離が極端(OSGi 等)
# Boot プロジェクトで spring-core が解決されているか
./gradlew dependencies --configuration runtimeClasspath | grep spring-core
# org.springframework:spring-core:5.3.x ← これがあれば CGLIB 機能あり
関連: Final クラス問題
CGLIB は対象クラスをサブクラス化するため、final クラスや final メソッドだとプロキシ化できません:
// ❌ NG: final クラス
@Configuration
public final class AppConfig { // ← Cannot subclass final class
@Bean
public Foo foo() { ... }
}
// ❌ NG: final メソッド
@Configuration
public class AppConfig {
@Bean
public final Foo foo() { ... } // ← Cannot subclass final method
}
// ✅ OK: 普通のクラス・メソッド
@Configuration
public class AppConfig {
@Bean
public Foo foo() { ... }
}
Kotlin の class は既定で final なので、open class にするか kotlin-spring プラグインを使用:
// Kotlin
@Configuration
open class AppConfig { // open 必須
@Bean
open fun foo(): Foo = Foo()
}
// または build.gradle.kts で
plugins {
kotlin("plugin.spring") version "1.9.0" // 自動 open
}
FAQ
Q: cglib と cglib-nodep、どちら?
A: nodep 版が安全。ASM を内包しているので、プロジェクトの ASM とバージョン衝突しません。
Q: Spring 5+ なのに出る
A: 1) 誰かが spring-core を除外、2) Maven shade plugin で誤って削除、3) クラスローダー分離問題、を確認。
Q: GraalVM Native Image で起動できない
A: CGLIB は動的バイトコード生成に依存し、Native Image と相性が悪いです。@Configuration(proxyBeanMethods = false) または Spring Native (Spring Boot 3 + AOT) を使用。
関連エラー
Cannot subclass final class— 対象クラスが final、Kotlin の場合はopen必須java.lang.NoClassDefFoundError: net/sf/cglib/proxy/Enhancer— CGLIB 自体が classpath に無い、対処1NoSuchMethodError: org.springframework.asm.ClassVisitor— Spring と ASM のバージョン不整合
子ページはありません
- 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 ... that could not be found.
- Annotation-specified bean name 'xxxController' for bean class [com.xxx.controller.xxxController] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.xxxController]
- 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
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- プロジェクトをTomcatプロジェクトとして認識させる方法 2026-10-07 22:32:50
- MySQLの1366 Incorrect string value|Laravelの文字コード・絵文字エラー 2026-10-07 21:54:03
- curlの証明書ホスト名不一致|旧エラー51・現行60の確認と対処 2026-10-07 21:54:03
- LaravelのMassAssignmentException|fillableの原因と安全な対処 2026-10-07 21:54:03
- Eclipse で Tomcat の起動ログがコンソールに出ない時の確認手順 2026-10-07 21:54:02
- MySQLにおける中央値(Median)の導き方(バージョン8未満) 2026-10-07 13:49:45
- getInputForward 2026-10-07 13:41:15
- JSONから配列に変換 2026-10-07 13:41:15
- ビューから値をモデルに格納しコントローラーで受け取る方法 2026-10-07 13:23:41
- Laravelのテーブル作成と定義変更|マイグレーション・up/down・注意点 2026-10-07 13:23:41
- NumPy 配列に要素を追加する方法 (append / concatenate) 2026-10-07 13:23:41
- MariaDB・MySQLで現在日時を取得する方法|NOW・タイムゾーン・保存型 2026-10-07 13:13:36
- 【django】テンプレートで定数を使用する方法 2026-10-07 13:10:15
- Spring BootにおけるApplication.propertiesの環境依存設定の分割方法 2026-10-07 12:09:35
- Not supported for DML operations【Springエラー】 2026-10-07 11:09:38