<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: LeoZhao</title>
    <description>The latest articles on DEV Community by LeoZhao (@leozhao).</description>
    <link>https://dev.to/leozhao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2306030%2Fa8067888-9a1a-4cec-a88e-249778e7c30d.png</url>
      <title>DEV Community: LeoZhao</title>
      <link>https://dev.to/leozhao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leozhao"/>
    <language>en</language>
    <item>
      <title>The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans</title>
      <dc:creator>LeoZhao</dc:creator>
      <pubDate>Mon, 18 Nov 2024 07:04:25 +0000</pubDate>
      <link>https://dev.to/leozhao/the-setapplicationcontext-method-execution-issue-of-the-applicationcontextaware-interface-and-172p</link>
      <guid>https://dev.to/leozhao/the-setapplicationcontext-method-execution-issue-of-the-applicationcontextaware-interface-and-172p</guid>
      <description>&lt;p&gt;In this article I would like to share a problem I encountered recently, which I think may be of interest to you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the issue?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The source code for both the testing and production environments is same, and both the local and testing environments run well, only the production environment run with an NPE of a service class failed to load, which added for the new requirements. This class inherits the interface from the &lt;code&gt;Customize&lt;/code&gt; package (a self-developed toolkit).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Project Structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzlzivl000qwep4agwsam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzlzivl000qwep4agwsam.png" alt="Image description" width="796" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The design of &lt;code&gt;Customize&lt;/code&gt; relies on Spring to manage APIs and services. With Spring automatic scanning, the common classes of &lt;code&gt;ApiEnhancer&lt;/code&gt; and &lt;code&gt;ServiceEnhancer&lt;/code&gt; are loaded when initializing classes, and obtain instances by &lt;code&gt;ApplicationContext&lt;/code&gt;. Debugging found that when loading them, the &lt;code&gt;ApplicationContext&lt;/code&gt; is null and has not been initialized yet. It is initialized in the &lt;code&gt;setApplicationContext&lt;/code&gt; method by implementing &lt;code&gt;ApplicationContextAware&lt;/code&gt;, so it is speculated that the &lt;code&gt;setApplicationContext&lt;/code&gt; method has not been executed.&lt;/p&gt;

&lt;p&gt;During the loading process of the &lt;code&gt;ApplicationContextProvider&lt;/code&gt; class, the static methods are loaded into the &lt;code&gt;Method Area&lt;/code&gt; during the initialization phase. However, when using the &lt;code&gt;ApplicationContext&lt;/code&gt; to get instances of beans, the static methods are directly invoked by the class name. As long as the API is created before the &lt;code&gt;ApplicationContextProvider&lt;/code&gt; been allocated memory in the heap and instantiated, the &lt;code&gt;setApplicationContext&lt;/code&gt; method will not be invoked to initialize.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;applicationContextProvider&lt;/code&gt; uses the annotation &lt;code&gt;@Component&lt;/code&gt;, and the Api uses the annotation &lt;code&gt;@RestController&lt;/code&gt;, and the two classes are in the same path. However, when Spring scans and loads them, there is no specific order, which means each of both classes may be created before another. In the production environment, the Api is created before the &lt;code&gt;applicationContextProvider&lt;/code&gt;, resulting that when the static method invoked directly by the class name to obtain beans, the &lt;code&gt;applicationContext&lt;/code&gt; has &lt;strong&gt;NOT&lt;/strong&gt; been initialized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solutions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;setApplicationContext&lt;/code&gt; method of the implementation class of the &lt;code&gt;ApplicationContextAware&lt;/code&gt; interface has not been executed, first check whether the implementation class has been set to &lt;code&gt;lazy loading&lt;/code&gt; or whether the project has configured global lazy loading.&lt;/p&gt;

&lt;p&gt;In this project, the problem was caused by the creation order of the implementation classes of the &lt;code&gt;ApiEnhancer&lt;/code&gt; and &lt;code&gt;ApplicationContextAware&lt;/code&gt; interfaces, and the Api class is just a regular RESTful API that handles business logic and can be loaded when invoked by the front-end page. Therefore, changing to &lt;code&gt;lazy loading&lt;/code&gt; solved the problem.&lt;/p&gt;

&lt;p&gt;It is advisable to minimize the dependency relationship between classes in terms of loading order. If unavoidable,  &lt;code&gt;lazy loading&lt;/code&gt;, or annotations such as &lt;code&gt;@DependsOn&lt;/code&gt;, &lt;code&gt;@Order&lt;/code&gt;, &lt;code&gt;@Priority&lt;/code&gt; can be used to control the loading order of beans.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Why are the creation order of production environment and testing environment different?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's debug to check the scanning process of Spring.&lt;/p&gt;

&lt;p&gt;Starting with the scan method of &lt;code&gt;ClassPathBeanDeterminationScanner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwhsbo1z8ki0bk4e4xi5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwhsbo1z8ki0bk4e4xi5x.png" alt="Image description" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;doScan&lt;/code&gt; method&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96job6cfa2i956zpgk5e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96job6cfa2i956zpgk5e.png" alt="Image description" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;scanCandidateComponents&lt;/code&gt; method of &lt;code&gt;ClassPathScanningCandidateComponentProvider&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawak4vmz8nzvyclzn5ns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawak4vmz8nzvyclzn5ns.png" alt="Image description" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;findAllClassPathResources&lt;/code&gt; method of &lt;code&gt;PathMatchingResourcePatternResolver&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzixhvjy8oiukcxx5ezhq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzixhvjy8oiukcxx5ezhq.png" alt="Image description" width="800" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;doFindPathMatchingJarResources&lt;/code&gt; method&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcwgjfnnbp48iox3uw0zr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcwgjfnnbp48iox3uw0zr.png" alt="Image description" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JarFile&lt;/code&gt; is a class in the Java Standard Library under the package &lt;code&gt;java.util.jar&lt;/code&gt;, which inherits and extends &lt;code&gt;ZipFile&lt;/code&gt;. &lt;code&gt;jarFile.entries()&lt;/code&gt; returns an iterator named &lt;code&gt;JarEntryIterator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp9601e6uhpk8mj8aidsb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp9601e6uhpk8mj8aidsb.png" alt="Image description" width="600" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JarExitIterator&lt;/code&gt; is an implementation of the &lt;em&gt;Iterator Pattern&lt;/em&gt;, which iterates on the entries of the parent class of &lt;code&gt;JarFile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftr0wc9in8sfokg513hnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftr0wc9in8sfokg513hnf.png" alt="Image description" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;entries&lt;/code&gt; method of &lt;code&gt;ZipFile&lt;/code&gt; return an iterator named &lt;code&gt;ZipExitIterator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqvhqbocnlokbipwfyqoz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqvhqbocnlokbipwfyqoz.png" alt="Image description" width="747" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;nextElement&lt;/code&gt; method of &lt;code&gt;ZipExitIterator&lt;/code&gt; invokes the &lt;code&gt;next&lt;/code&gt; method, which in turn invokes the &lt;code&gt;getNextEntry&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnij0zz0pct28bjzpvjh0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnij0zz0pct28bjzpvjh0.png" alt="Image description" width="732" height="711"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getNextEntry&lt;/code&gt; is a &lt;strong&gt;native&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcor00xlub2duetdion76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcor00xlub2duetdion76.png" alt="Image description" width="731" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The native method is implemented in non Java languages and invoked within the Java Virtual Machine to implement underlying functionality, which may vary depending on the environment (Operating System or JDK version). JAR packages themselves do not have an order, so the actual traversal order may vary depending on different JAR packaging tools and environments.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>applicationcontextaware</category>
      <category>applicationcontext</category>
    </item>
  </channel>
</rss>
