1

I am working on some Java projects using Spring boot. I have an autoconfigured class MyAutoConfig in the spring.factories file in a project A.

This configuration file has some bean definitions, for example the class Worker.

The project B is using A as a library, in gradle like this:

implementation project(':project-A')

I can start project B without any errors. But when I try to use @Autowired Worker worker in project B in the main class where I have @SpringBootConfiguration, it told me that "expected at least 1 bean which qualifies as autowire candidate", so I assume that the bean is not in the context.

I would like to know what could be the problem ?

hawarden_
  • 1,904
  • 5
  • 28
  • 48
  • hmm, what if you build the jar and try to start it up via `java -jar`? In case this does not work either, you could unpack the jar and look what is actually inside it, that could shed some light. – Eugene Oct 28 '21 at 21:59
  • what should I look for ? You mean in the jar of project B ? – hawarden_ Oct 28 '21 at 22:06

1 Answers1

1

@SpringBootConfiguration is not enough for the auto-configuration. You should add @EnableAutoConfiguration. This will trigger scanning spring.factories files and do the rest for you.

Or using @SpringBootApplication, because it has EnableAutoConfiguration in it.

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication
Linh Vu
  • 736
  • 3
  • 7