Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ComponentScan order matter?

I'm setting up a very small Spring/REST/JPA project with Boot, using annotations.

I'm getting some Bean not found errors in my REST controller class that has an Autowired repository variable, when I move my JPA repository class out to a different package, and calling componentscan on its package. However, everything was working fine when all my files(5 total) were in the same package.

So I was wondering, however unlikely, if the component scan order matters? For example, if a class is AutoWiring some beans from a package that has not been 'component scanned' yet, will that cause a Bean not found error?

like image 627
JeffLL Avatar asked May 28 '26 21:05

JeffLL


2 Answers

No, Spring loads all configuration information, from files and annotations and the environment when appropriate. It then creates beans (instances of classes) according to a dependency tree that it calculates in memory. In order to do this it has to have a good idea of the entire configuration at startup. The whole model derived from all the aggregated configuration information is called the Application Context.

In modern versions of spring the application context is flexible at runtime and so it's not quite the case that all the configuration is necessarily known up front, but the configuration that is flexible is limited in scope and must be planned for carefully.

like image 96
Software Engineer Avatar answered May 31 '26 13:05

Software Engineer


Maybe you need to share some code. When you move that stuff, you also need to tell Spring where they went. My guess would be you haven't defined @EntityScan and @EnableJpaRepositories (which default to the location of @EnableAutoConfiguration).

like image 45
Dave Syer Avatar answered May 31 '26 14:05

Dave Syer