6

I have 3 spring-boot-starter projects

One of the autoconfiguration class has the following code:

@Configuration
@ConditionalOnClass(value = Config.class)
@AutoConfigureAfter(value = {FileGeneratorConfig.class, FileUploaderConfig.class})
public class JobConfig 

FileGeneratorConfig and FileUploaderConfig are also autoconfiguration classes.

I was expecting that beans created in FileUploaderConfig will be created first. So test this I had put a break point in the method that creates bean in JobConfig and FileUploaderConfig. But the break point hits JobConfig first which makes me believe that my @AutoConfigureAfter is not working. Is that the right assumption.

Also in FileUploaderConfig i have this:

@Bean
    FileUtilContainer fileUtilContainer(FileUtilContainerProperties fileUtilContainerProperties){
        return new FileUtilContainer(FileUtil.createDirectory(fileUtilContainerProperties.getArchive()),
                                     FileUtil.createDirectory(fileUtilContainerProperties.getWorking()),
                                     FileUtil.createDirectory(fileUtilContainerProperties.getConfirmation()), 
                                     FileUtil.createDirectory(fileUtilContainerProperties.getConfirmationProcessed()),
                                     FileUtil.createDirectory(fileUtilContainerProperties.getError()), 
                                     FileUtil.createDirectory(fileUtilContainerProperties.getErrorProcessed()));
    }

and FileUtilContainerProperties:

@Component
@ConfigurationProperties(prefix = "batch.letter.directory", ignoreUnknownFields = false)
public class FileUtilContainerProperties

but it is not creating FileUtilContainerProperties bean. Am I missing something here?

sachin jain
  • 224
  • 1
  • 4
  • 16

1 Answers1

6

AutoConfigureAfter controls the order in which the configuration files are processed and their bean definitions are created. The order in which beans are created from those definitions is a separate concern and depends on, among other things, the dependencies that exist between your beans.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • thanks that makes sense. What about the second part where @Component is not working. FileUtilContainerProperties was not added to the context? – sachin jain Feb 16 '17 at 23:04
  • I'd guess it's in a package that isn't being component scanned. It's best to avoid relying on component scanning in your auto-configuration. I'd use `@EnableConfigurationProperties(FileUtilContainerProperties.class)` on`FileUploaderConfig` instead. – Andy Wilkinson Feb 16 '17 at 23:13
  • Thanks Andy I used that and it works fine. So I guess as a rule of thumb when using AutoConfiguration we should avoid component scanning and use @EnableConfigurationProperties right? – sachin jain Feb 17 '17 at 13:20