Spring boot autoconfiguration from a generated source

I’m in the process of creating a custom openapi-generator, to auto generate configuration properties and (hopefully) AutoConfiguration.

I have it generating everything into target/generated-sources/demo-client.

It generates the following structure:

└── src
    └── main
        ├── AndroidManifest.xml
        ├── java
        │   └── com
        │       └── demo
        │           └── custom
        │               ├── ApiClient.java
        │               ├── ...
        │               ├── api
        │               │   ├── PetApi.java
        │               │   ├── StoreApi.java
        │               │   └── UserApi.java
        │               ├── config
        │               │   ├── DemoApiClientAutoConfiguration.java
        │               │   └── DemoApiProperties.java
        │               └── model
        │                   ├── Address.java
        │                   └── ...
        └── resources
            └── META-INF
                ├── spring
                │   └── org.springframework.boot.autoconfigure.AutoConfiguration.imports
                └── spring.factories

And the classes are as follows:

DemoApiProperties.java:

@Component
@ConfigurationProperties("m2m.demo")
public class DemoApiProperties {

    private String baseUrl = "demo-service";

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

}

DemoApiClientAutoConfiguration.java:

@AutoConfiguration
@ConditionalOnProperty("m2m.demo.base-url")
public class DemoApiClientAutoConfiguration {

    @Bean
    public ApiClient demoM2mApiClient(RestClient m2mRestClient, DemoPackageApiProperties apiProperties) {
        ApiClient apiClient = new ApiClient(demoM2mApiClient);
        apiClient.setBasePath(apiProperties.getBaseUrl());
        return apiClient;
    }

    @Bean
    public PetApi petApi(ApiClient demoM2mApiClient) {
        return new PetApi(demoM2mApiClient);
    }

    @Bean
    public StoreApi storeApi(ApiClient demoM2mApiClient) {
        return new StoreApi(demoM2mApiClient);
    }

    @Bean
    public UserApi userApi(ApiClient demoM2mApiClient) {
        return new UserApi(demoM2mApiClient);
    }


}

And then the META-INF files:

org.springframework.boot.autoconfigure.AutoConfiguration.imports

com.demo.custom.config.DemoApiClientAutoConfiguration

And as you can see I’ve even tried in the spring boot two way, using spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.demo.custom.config.DemoApiClientAutoConfiguration

But whenever I start the application it is not finding the DemoApiClientAutoConfiguration so it isn’t processing it, from looking at the debug logs Spring Boot isn’t discovering that this AutoConfiguration exists.

If I update my application to use Imports it works fine:

// this is exactly the same class as whats in the AutoConfiguration imports
import com.demo.custom.config.DemoApiClientAutoConfiguration; 

@SpringBootApplication
@Import({
  DemoApiClientAutoConfiguration.class
})
public class MyServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeeEngineApplication.class, args);
    }

}

I am using Spring Boot 3.4.0.

Is it possible for Spring to pickup generated-sources AutoConfiguration‘s? It’s not the end of the world if we have to use @Import, would just be nice to remove that extra verbosity.

I think you are missing the EnableConfigurationProperties Annotation on the Autoconfiguration class.

It should be:

@AutoConfiguration
@ConditionalOnProperty("m2m.demo.base-url")
@EnableConfigurationProperties(DemoApiProperties.class)
public class DemoApiClientAutoConfiguration {

    @Bean
    public ApiClient demoM2mApiClient(RestClient m2mRestClient, DemoPackageApiProperties apiProperties) {
        ApiClient apiClient = new ApiClient(demoM2mApiClient);
        apiClient.setBasePath(apiProperties.getBaseUrl());
        return apiClient;
    }

    @Bean
    public PetApi petApi(ApiClient demoM2mApiClient) {
        return new PetApi(demoM2mApiClient);
    }

    @Bean
    public StoreApi storeApi(ApiClient demoM2mApiClient) {
        return new StoreApi(demoM2mApiClient);
    }

    @Bean
    public UserApi userApi(ApiClient demoM2mApiClient) {
        return new UserApi(demoM2mApiClient);
    }

Be sure to have the property m2m.demo.base-url in your configuration file in order for this to work, i would also recommend to change that and add a boolean property to enable/disable the configuration and use it instead of the url.


Spring boot 3.x autoconfiguration doc

https://docs.spring.io/spring-boot/reference/features/developing-auto-configuration.html#features.developing-auto-configuration

in short: don’t use META-INF/spring.factories but META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật