Location>code7788 >text

In-depth explanation: Spring.3 version of the evolution and practice of automatic assembly mechanism

Popularity:24 ℃/2024-09-19 11:37:04

preamble

Today, we will kick off our explanation of the Spring-AI source code series. Please don't be in a hurry, I will go deeper gradually and focus on one knowledge point at a time in case it gets confusing.

First of all, the discussion of the source code is naturally inseparable from the automatic assembly. Some people may ask, this content has been explained before, why do we need to talk about it again? This is because since Spring Boot version 3.0, the mechanism of automatic assembly has undergone some changes. Nevertheless, with the source code reading skills we already have, today we will briefly review how the new version of Spring handles auto-assembly. After all, as versions continue to be upgraded, we must adapt to the new mechanisms and avoid still reading the source code with the old mindset.

version dependency

To explore Spring-AI in depth, you first need to configure several key dependencies. Below are the essential core dependencies:

<parent>
        <groupId></groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.1</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency> 
      <groupId></groupId>
      <artifactId>spring-ai-zhipuai-spring-boot-starter</artifactId>
      <version>1.0.0-M2</version>
</dependency>
<dependency> 
      <groupId></groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>1.0.0-M2</version>
      <type>pom</type>
      <scope>import</scope>
</dependency>

After completing the introduction, we will be able to view the source code logic smoothly. Also, be sure to download the relevant documentation for better understanding and analysis.

automated assembly

As we have discussed before, SpringBoot's auto-assembly mechanism looks for the META-INF/ file within the package by default. The following shows the assembly source code of an earlier version.

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = (getSpringFactoriesLoaderFactoryClass(),
            getBeanClassLoader());
    (configurations, "No auto configuration classes found in META-INF/. If you "
            + "are using a custom packaging, make sure that file is correct.");
    return configurations;
}

Here.loadFactoryNames method will look for theMETA-INF/ file. However, when I looked for the source code of the auto-assembly, I was unable to find this file after much effort.

image

This was obviously problematic and the first step was bogged down. So, I went back to the drawing board and re-found the source code for the auto-assembly. This time, I replaced the files I was checking, and here is the source code in question:

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
  List<String> configurations = (, getBeanClassLoader())
      .getCandidates();
  (configurations,
          "No auto configuration classes found in "
                  + "META-INF/spring/. If you "
                  + "are using a custom packaging, make sure that file is correct.");
      return configurations;
}

Eventually, I discovered the source of the problem. It turns out that the change here toMETA-INF/spring/ file. This adds another file name to the auto-assembly mechanism that needs to be memorized. Fortunately, the name is not too difficult to remember. The next step was to look at it and realize that the actual contents were similar to the previous file.

image

Okay, the preparation of the assembly has been fully accomplished. The next sections will delve into the finer details.

summarize

In today's presentation, we started from the source code of Spring-AI and gradually deconstructed the evolution and practice of auto-assembly. Although the core concepts of auto-assembly haven't changed much since the past, the mechanism adjustments brought by Spring Boot version 3.3 are really worth revisiting. By reviewing the new version of the automatic assembly implementation, we not only deepen our understanding of the new mechanism, but also lay a solid foundation for subsequent source code analysis.

As versions iterate, keeping a keen eye on new technologies will be the key to our continuous exploration and progress. We hope you can follow our steps, deeply comprehend every knowledge point, and eventually find your own piece of the star in the vast universe of source code.


I'm Rain, a Java server-side coder, studying the mysteries of AI technology. I love technical communication and sharing, and I am passionate about open source community. I am also a Tencent Cloud Creative Star, Ali Cloud Expert Blogger, Huawei Cloud Enjoyment Expert, and Nuggets Excellent Author.

💡 I won't be shy about sharing my personal explorations and experiences on the path of technology, in the hope that I can bring some inspiration and help to your learning and growth.

🌟 Welcome to the effortless drizzle! 🌟