Spring boot custom starter org.springframework.boot.autoconfigure.AutoConfiguration.import not detecting configuration classes version 2.7.2
Asked Answered
B

2

7

I've read that I should not open an issue on github so I ask here. I've digged into the code and for example spring-boot-actuator-autoconfigure doesn't define the @Configuration\@AutoConfiguration classes inside META-INF/spring.factories follow the content of the file:

org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer

I've checked and ValidationFailureAnalyzer is not even annotated with @Configuration\@AutoConfiguration. Then I see the file META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports declaring all the classes @AutoConfiguration follow a little extraction of the file:

org.springframework.boot.actuate.autoconfigure.amqp.RabbitHealthContributorAutoConfiguration
org.springframework.boot.actuate.autoconfigure.audit.AuditAutoConfiguration
org.springframework.boot.actuate.autoconfigure.audit.AuditEventsEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.availability.AvailabilityHealthContributorAutoConfiguration
...

all these classes are annotated with @AutoConfiguration. So far so good If we read the docs they say that:

Spring Boot checks for the presence of a META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file within your published jar.

Indeed if we import:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.7.3</version>
</dependency>

everything works just fine. I'm not skilled with gradle but I don't see any special dependecy in spring-boot-actuator-starter or spring-boot-actuator-autoconfigure. Searching on google I've found a discussion here where they say:

In Spring Boot v. 2.7 auto-configuration registration is moved from spring.factories to a new file named META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each line contains the fully qualified name of the auto-configuration. For backwards compatibility, entries in spring.factories will still be honored.

But honestly I've tryed to move the configuration classes in the new file but the configuration class is not loaded. I've writed an example here. My org.springframework.boot.autoconfigure.AutoConfiguration.imports file:

com.example.springbootstarterexample.configuration.Config

If I move to the old configuration spring.factries everything works fine.

My @AutoConfiguration class:

@AutoConfiguration(after = JpaRepositoriesAutoConfiguration.class)
//@AutoConfigureAfter(JpaRepositoriesAutoConfiguration.class)
@EnableJpaRepositories(basePackages = "com.example.springbootstarterexample.repository")
@Import({SomeServiceImpl.class, SomeEntityController.class})
public class ExampleAutoConfiguration {

} 

am I doing something wrong? why the spring-boot-starter-actuator works and my spring-boot-starter-example dosn't?

Barbule answered 25/8, 2022 at 9:9 Comment(4)
I'm also searching for info on it to start some tests. Assure you didn't forget to put the @AutoConfiguration annotation on the top-level of your auto-configuration classes as mentioned here. I think that this is mandatory for the new imports file to work.Indiscreet
I’ve tryed everything. @Configuration\@AutoConfoguration , copiy the dependency in the pom of spring-boot-starter-actuator/auto configure, refactor the package of the configuration class with autoconfiguration in it. Nothing workedBarbule
Tryed also to remove all annotation from ExampleAutoConfiguration leaving only @AutoCOnfiguration and declaring a single bean, still the configuration is not loadedBarbule
I have made some tests yesterday and I was able to make it work. I will provide a sample in a github public repository but I am using kotlin with gradle and not java with maven.Indiscreet
K
9

Your file is called org.springframework.boot.autoconfigure.AutoConfiguration.import,

and must be org.springframework.boot.autoconfigure.AutoConfiguration.imports (notice the extra s) at the end.

Kirbykirch answered 30/8, 2022 at 5:2 Comment(5)
I’ve not access to my laptop right now. After I test I will accept don’t worry. I’m not that kind of users…Barbule
Anyway was you able to setup a starter with entities in it? I’ve tryed to load entities in the starter with @Import But it doesn’t work. The only way is to use @EntityScan in the consumer because if also in the consumer we have entities we declare all the packages of the entities. But this breaks the autoconfiguration meaning. If it’s off tipici I will ask another question. I wonder how spring-boot-batch-starter Works, because it uses some tables in order to make the batch workBarbule
@Barbule I did not start that project, no, just skimmed over it... you can ask a separate question and I'l take a lookKirbykirch
the question is here if interested @KirbykirchBarbule
that's important to mention that format is different now, here is an example github.com/spring-projects/spring-boot/blob/main/…Porras
T
1

Put the file org.springframework.boot.autoconfigure.AutoConfiguration.imports under resource/META-INF/spring package-> ~ProjectName/src/main/resources/META-INF/spring/

This file then can contain your custom configuration file fully qualified name like example below com.test.autoconfigure.MongoApplicationDBConfig

Transistor answered 21/9, 2023 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.