spring boot automatic assembly injection failed, spring boot custom starter is not initialized, spring boot automatic assembly is invalid, not executed

1. Problem description

I customized a bunch of starters, packaged them into jars, and imported them into the main program. When I started, I reported an error. As long as I imported the bean in the custom starter, I would report that the bean could not be found, that is, it was not scanned by spirng. , search for the following keywords:

spring boot automatic assembly injection failed, spring boot custom starter is not initialized, spring boot automatic assembly is invalid, not executed

Two, the solution

I searched around the Internet, but to no avail. Basically, it is said that the package where the startup class is located causes something. Add scanBasePackages to specify the package name in the @SpringBootApplication annotation:

@SpringBootApplication(scanBasePackages = "com.aaa.bbb")	

My startup class itself is placed under the aaa package, and there is no such problem at all. I looked around on the Internet, but I didn't find any valuable answers. What is confirmed at present is that this problem must be caused by automatic assembly, and there is no initialization. , and then searched again, the principle of automatic assembly, looked at it, and then found the problem through Debug, and found the answer in the org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getCandidateConfigurations method. It is prompted in the assertion
insert image description here
:

No auto configuration classes found in META-INF/spring.factories. If you

Then I suddenly remembered that this is a spring boot program. The jar package that is typed out is different from the ordinary jar package. Add the following dependencies

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
		<fork>true</fork>
		<mainClass>com.xxxx.xxxx</mainClass>
	</configuration>
	<executions>
		<execution>
			<goals>
				<goal>repackage</goal>
			</goals>
		</execution>
	</executions>
</plugin>				
				

Recompile the following and it will work.

If you are not sure whether it is the reason for automatic assembly, you can import the configuration registered by the starter into the main program through @Import on the startup class, and see if it still prompts that the injection fails.

@Import({
    
    xAutoConfiguration.class, xxxxAutoConfiguration.class})

In fact, it’s okay, directly set the log level of spring to debug, and confirm the problem by tracking the log, but there are a lot of logs, which is a bit messy.


2021-1-15 update:

This afternoon, the automatic assembly failed again. It took me a long time to track the source code and adjust it, but the automatic assembly was not loaded. In the lib directory of the jar package that was typed out, there was the jar, but it was not loaded. In org The configurations variable of the .springframework.boot.autoconfigure.AutoConfigurationImportSelector#getCandidateConfigurations method always does not have the automatic assembly class in the jar, and there are others, that is, the
insert image description here
executable jar that is not typed by the jar is no problem, and it can be executed by command, but It doesn’t work in IDEA, and it prompts that a bean in the jar is not injected. First of all, whether the jar exists in the dependency or
insert image description here
not, add it, I have it, exclude it, and finally found the problem in the first line of log of idea , when IDEA starts, it will specify a classpath by itself

insert image description here
Add all dependencies of the project to this
insert image description here

The jar I was missing happened to not be here, so it didn’t work, and it couldn’t be loaded. I cleared IDEA’s cache, restarted and found that it still didn’t work, and finally found that it would be fine to rebuild. . . drunk drunk

insert image description here

Guess you like

Origin blog.csdn.net/mashangzhifu/article/details/122462361