Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Component Scan not scanning sub packages

I have encountered a strange problem. I am under the impression that component scan scans the sub packages recursively if a top level package is specified for scanning.

My repositories and entities are the maven dependency of the project. They live under the package name com.foo.bar.xyz and my application config is under package com.foo.bar. When I write @ComponentScan(basePackages = "com.foo.bar"), along with @EnableJpaRepositoriesit gives an error that repository bean not found.

However when I specify a top level repository package like @EnableJpaRepositories(basePackages = com.foo.bar.xyz) , along with component scan as above, it detects the repository just fine.

Now is this happening only because the repositories and entities are being injected as maven dependency? So does the recursive part of component scan, scans the sub packages or the subdirectories?

like image 534
Nikhil Sahu Avatar asked Jun 20 '16 06:06

Nikhil Sahu


People also ask

How do I put multiple packages in component scan?

To add many packages to Component Scan, you should pass the String[] array to the @ComponentScan annotation.

What is the correct way to enable component scanning?

Auto Components ScanningPut this “ context:component ” in bean configuration file, it means, enable auto scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.

Does ComponentScan scan @configuration?

While developing an application, we need to tell the Spring framework to look for Spring-managed components. @ComponentScan enables Spring to scan for things like configurations, controllers, services, and other components we define.

How do you scan the components in the same package of a Spring boot application to instantiate the dependent classes?

Using @ComponentScan in a Spring Application. With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.


1 Answers

Now is this happening only because the repositories and entities are being injected as maven dependency?

  • No it is not

So does the recursive part of component scan, scans the sub packages or the subdirectories?

  • Yes component scan does search recursively in sub packages

To elaborate here @ComponentScan is intended to search all classes having @Component or its sub types like @Controller whereas to enable Spring Data JPA by annotating the PersistenceContext class with the @EnableJpaRepositories annotation and to configure the base packages that are scanned when Spring Data JPA creates implementations for the repository interfaces. Hence the need for declaring the base package information for both @ComponentScan and @EnableJpaRepositories

like image 104
Mudassar Avatar answered Sep 29 '22 01:09

Mudassar