Home > Net >  What is the relationship between Spring Boot and the Maven pom.xml file?
What is the relationship between Spring Boot and the Maven pom.xml file?

Time:01-11

I use maven to build spring boot applications. I know that the maven spring-boot dependencies have annotations (such as @AutoConfiguration or @Condition*). At what point in the SpringApplication.run method in the main class does it actually read from the pom.xml file? I'm stepping through the code line by line and I can't figure out at what point does Spring Boot interact with the pom.xml file.

The @EnableAutoConfiguration enables auto-cofiguration of the Application Context based on jar files in the classpath and user-defined beans, so presumably the pom.xml eventually gets added as a source and the annotations in the dependencies are read when the auto-configuration state is running, but I can't figure out where or when it does so.

CodePudding user response:

SpringApplication.run doesn't read the pom.xml file, it works at a higher level of abstraction. The pom.xml file is used by your IDE or by the mvn command line application to download dependencies and configure your applications classpath.

By the time that SpringApplication.run is called, the classpath is fully configured but SpringApplication itself isn't aware about how this was done.

Auto-configuration occurs by searching the all jars on the classpath looking for a files named META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (if you're using a recent version). Once these files have been found, the classes listed within them are loaded and used for configuration.

If you want to step though the code, you can set a breakpoint at AutoConfigurationImportSelector.getCandidateConfigurations(...) and see the auto-configuration candidate classes get found. The exact auto-configurations that get applied will depend on the @Condition... annotations on them.

  • Related