Search code examples
javaspringconfigurationspring-annotations

Load entire folder of Java Configuration files?


Is there any way to load a entire folder with @Configuration files, the same way XML have: <import resource="folder/*.xml" />, but with annotations?.


Solution

  • You could probably use component scanning.

    @Configuration
    @ComponentScan(basePackages = "org.example.configs")
    public class Config {
    }
    

    All @Configuration classes from the org.example.configs package will be included in the context.

    Typesafe alternative:

    // org.example.configs.SubConfig
    @ComponentScan(basePackageClasses = SubConfig.class)