Interviewer asked, How will you scan packages which are not in the src/main/java .. ie.. your classpath. He also said that this is very rare situation when you have to do this type of things.
-
Possible duplicate of [Adding files to java classpath at runtime](https://stackoverflow.com/questions/1010919/adding-files-to-java-classpath-at-runtime) – Vinay Prajapati Oct 23 '19 at 06:04
-
Hi there, you can refer this link https://www.baeldung.com/spring-component-scanning – sovannarith cheav Oct 23 '19 at 06:05
3 Answers
I think you need to add folders manually into your java build path before adding them into component scan. If you are using Eclipse, there is a way to add them in build path by right clicking on project -> properties -> java build path -> source. You need to add the path to src/<path to your additional source>
here and enable it so that it is recognized as a source folder. Then you need to use @ComponentScan to scan for the relevant packages. Key thing here is to get your relevant code into the classpath.
One scenario i can think of where you would potentially need such kind of changes would be having some generated sources under target/generated-sources
and you need to use those stubs or beans to develop your code.

- 1,340
- 1
- 17
- 32
In spring-boot by default all classes under the package where the main application class resides are component scanned. For instance suppose your main application.java
is under src/main/java
and in package com.example
. So springboot will scan all classes under the package com.example.*
So if your other package hierarchies are below your main app with the @SpringBootApplication
annotation, you’re covered by implicit components scan.
If there are beans/components in other packages which are not sub packages of the main package, you should manually add them as @ComponentScan
like :
@ComponentScan({“com.example1”,”com.example2”})
@SpringBootApplication
public class SpringbootMainApplication {
public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(SpringbootMainApplication.class, args);
}
}
Apart from this the @ComponentScan
annotation can also be provided along with @Configuration
annotated classes to tell spring to scan packages. @ComponentScan
without arguments tells Spring to scan the current package and all of its sub-packages.Like :
@Configuration
@ComponentScan
public class SpringGenericConfiguration {
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
}
Suppose the class SpringGenericConfiguration.java
is inside package com.example20.configuration
, then spring will scan all classes in the package and in the sub packages if any.

- 5,706
- 6
- 22
- 39
-
I think the code above will add all the components specified in the package under src/main/java. I believe the OP asked for ways to include code that is there in a path other than src/main/java – Kavitha Karunakaran Oct 23 '19 at 06:22
-
@KavithaKarunakaran The build tools like maven and gradle expects a basic directory structure for us to be followed . And by convention all java codes are generally put under `src/main/java` .If the classes are compiled correctly by the build tools and find their way into the jar or war then only will the component scan annotation can work when spring starts up. – Ananthapadmanabhan Oct 23 '19 at 06:29
-
I agree to what you said. But in a case when you need some stubs that are created at runtime while you run generated sources goal, you need to add them to classpath and then only it can work. – Kavitha Karunakaran Oct 23 '19 at 06:33
-
@KavithaKarunakaran Seems we both have different understanding of the question at hand :) – Ananthapadmanabhan Oct 23 '19 at 06:45
-
-
1If you add `@SpringBootApplication` then it's best not to add `@ComponentScan` but add the parameter `scanBasePackages` to `@SpringBootApplication` instead. – herman Jun 16 '20 at 12:01
The simplest and shortest answer is that you scan them using exactly the same way, it doesn't matter if the class or package is inside src/main/java, at least it should be in classpath so JVM knows about it and has loaded it.
IF the OP's question means that the package is not at the classpath at all by saying:
which are not in the src/main/java .. ie.. your classpath
The question cannot make sense, why to scan a package that is not in JVM classpath. Even if you could scan it you wouldn't be able to use it as it is not loaded.
-
In principle, it would be possible to manually read the .class file into a byte array, then load the class dynamically that way. I don't think it would be good to actually do this in real software, but it is possible to do. Given that this was asked in an interview, "it doesn't make sense" or "you wouldn't be able to" aren't correct answers. – kaya3 Nov 05 '19 at 13:50
-
I aggree totally. My point was that someone can read differently the OP's question, the part: not in src/main/java ie your classpath. I am not sure if it means not in src/main/java nor in classpath or not in src/main/java but in classpath. – Java Learner Nov 05 '19 at 14:01
-
Either way, it is possible to open the file from the filesystem, read it into a byte array, and load the class that way. – kaya3 Nov 05 '19 at 14:03