Solve Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigExce

Table of Contents

Solve Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.alibaba.alibrain.quotareport.controller.QuotaReportDayController

Problem Description

solution

1. Check the visibility of a class

2. Exclude final classes

3. Check Spring version

4. Use other proxy modes

Summarize


Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.alibaba.alibrain.quotareport.controller.QuotaReportDayController: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.alibaba.alibrain.quotareport.controller.QuotaReportDayController at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)

Solution to Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.alibaba.alibrain.quotareport.controller.QuotaReportDayController

Problem Description

When using the Spring framework to create beans, you may encounter error messages similar to the following:

plaintextCopy codeInitialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.alibaba.alibrain.quotareport.controller.QuotaReportDayController: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.alibaba.alibrain.quotareport.controller.QuotaReportDayController
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)

This error usually indicates that the CGLIB subclass cannot be generated when creating the bean. The reason may be the use of final classes or non-visible classes.

Solution

1. Check the visibility of the class

First, we need to make sure that the class declared as a Bean is visible. In Java, the visibility of a class is determined by ??public??, ??protected??, ??private?? and default ( No modifier) four levels of control. If the class declared as a Bean is non-public (that is, it is not ??public??modified), please ensure that the package in which the class is located is under Spring’s scan path and is configured correctly. Packet scanning rules. Also, you should check that the class’s modifiers are correct. Spring cannot create CGLIB proxy objects targeting final classes or non-inherited classes (such as interfaces).

2. Exclude final classes

If you encounter a Cannot subclass final class error, it means that the class declared as a Bean is a final class. Final classes cannot be inherited, so CGLIB proxies cannot be created. To solve this problem, there are several ways:

  • If you wrote the class yourself, remove the final modifier and recompile.
  • If you are using a class provided by a third-party library, you can try to use other inheritable classes or interfaces provided by the library.
  • If you cannot modify the class declared as a Bean, you can consider using a JDK dynamic proxy instead of a CGLIB proxy. JDK dynamic proxy can be enabled by setting ???? in the Spring configuration file.

3. Check Spring version

Sometimes, this issue may be caused by Spring version incompatibility. Please make sure that the version of Spring you use is compatible with your project and dependencies. If you are using an older Spring version, you can try upgrading to the latest version to see if the issue is resolved.

4. Use other proxy modes

In addition to CGLIB proxies, Spring also supports other proxy modes such as JDK dynamic proxies and AspectJ proxies. You can try using JDK dynamic proxy or AspectJ proxy instead of CGLIB proxy. In the Spring configuration file, you can enable JDK dynamic proxy or AspectJ proxy by setting????.

Summary

Solving the error that Spring cannot generate CGLIB subclasses can be solved by excluding the final class, checking the visibility of the class, upgrading the Spring version, or using other proxy patterns. Choosing the right solution based on the situation can successfully solve this problem.

Here’s a sample code showing how to solve the problem:

javaCopy codepackage com.example.demo.controller;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
    //The specific implementation code of Controller
}

In this example, we have a MyController class used to handle requests, declared with the @Controller annotation. If we get an error like this when running:

plaintextCopy codeInitialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.example.demo.controller.MyController: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.example.demo.controller.MyController

There are several steps we can take to resolve this issue:

  1. First, make sure the MyController class is visible. If the class is under a package but the package is not under Spring’s scan path, you can solve this problem by adding package scanning rules in the configuration file.
  2. If the class ??MyController?? is declared as ??final??, we need to remove the ??final?? keyword because Spring cannot generate a CGLIB subclass for the ??final?? class. The modified code is as follows:
javaCopy codepackage com.example.demo.controller;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
    //The specific implementation code of Controller
}
  1. If the modification is not feasible, we can try to use JDK dynamic proxy or AspectJ proxy to solve the problem. In the Spring configuration file, configure the following proxy mode to enable JDK dynamic proxy:
xmlCopy code<aop:aspectj-autoproxy proxy-target-class="false"/>

In this way, Spring will use the JDK dynamic proxy instead of the CGLIB proxy to create beans. It should be noted that JDK dynamic proxy can only proxy classes that implement interfaces. Through the above steps, we can solve the problem of Spring initialization bean failure caused by using the ??final?? class or non-visible class, so that our application can run normally.

CGLIB (Code Generation Library) is a library based on Java bytecode generation and operation. It can generate subclasses at runtime to implement the proxy for the target class. CGLIB proxies are primarily used to create and use dynamic subclasses of Java classes at runtime. The main features and uses of CGLIB agents are as follows:

  1. Inherited proxy: CGLIB proxy implements proxy by creating a subclass of the target class. This subclass inherits all methods and fields of the target class, and can add additional methods, logic, and state to it.
  2. No interface required: Compared with JDK dynamic proxy, CGLIB proxy does not require the target class to implement an interface. CGLIB can proxy any class, whether it is an interface, abstract class or ordinary class.
  3. Higher performance: Since the CGLIB proxy directly inherits the target class and generates subclasses, it avoids the performance overhead of calling through the interface. Compared to JDK dynamic proxies, CGLIB proxies generally have higher performance.
  4. Richer functions: The CGLIB proxy can perform more flexible operations on the target class, such as adding additional logic before and after method execution, modifying method return values, etc.
  5. Relies on bytecode generation: The CGLIB agent needs to generate a subclass of the target class at runtime to implement the agent. This means that bytecode manipulation of the target class is required and may therefore be restricted in some environments, such as being unavailable under certain security management mechanisms.
  6. Final classes and methods are not supported: Since CGLIB proxies implement proxies by inheriting the target class, final classes and final methods cannot be proxied. When using the Spring framework, when we configure the class-based proxy mode (proxy-target-class=”true”), the CGLIB proxy is used by default to create the Bean proxy object. This makes it easy to perform AOP operations on beans, such as intercepting method calls, transaction management, etc. Sample code using the CGLIB proxy is as follows:
javaCopy code// target class
public class MyService {
    public void doSomething() {
        //Method implementation code
    }
}
//Agent generation
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyService.class);
enhancer.setCallback(new MyMethodInterceptor());
//Create proxy object
MyService proxy = (MyService) enhancer.create();
// Call method through proxy object
proxy.doSomething();

In the above example, we created a proxy object ??proxy?? of ??MyService?? and passed ??Enhancer?? The ??create()?? method of the class generates a subclass of the target class ??MyService??, which inherits the methods of the target class and is ??MyMethodInterceptor??The enhanced logic of the method is implemented in the callback object. In short, CGLIB proxy is a technology that generates subclasses at runtime to implement proxy for target classes. It has the characteristics of inherited proxy, no interface, higher performance and richer functions. In the Spring framework, CGLIB proxy is widely used to implement AOP operations, providing convenient and powerful proxy functions.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138781 people are learning the system