Preface

The title may be confusing, just go to the example


If we are in the code, execute the following

server instanceof MvpServer

What will it return? If you get the correct answer, you don’t need to read the content below.

the answer is

When I saw this answer, I was really confused, and I suspected that it was my idea.

Troubleshoot

For problems like this, sometimes it really depends on the usual accumulation. What is the load of the class? The answer is the class loader, even if the class package name is the same, the class name is the same, if you are loaded by a different class loader, the class is also different. So we can first check from the class loader


在这里插入图片描述
It is easy to see from the picture that the class loader of server and MvpServer are different. The server class loader is provided by the spring devtools package. Therefore, the solution is very simple, just check whether it is introduced in the project

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

If there are, you can directly remove them to avoid impact on the project. If you really don’t want to remove it, you can set the current class loader of the entire thread to the spring-boot-devtools class loader, which can be passed

  Thread.currentThread().setContextClassLoader();

When we change the class loading to the same, let's look at the effect



Summarize

Maybe we usually memorize some eight-legged essays, class loaders such as boostrap classloader, ext classloader, app classloader, etc., but sometimes if we really troubleshoot the problem, we will ignore it.


linyb极客之路
344 声望193 粉丝