1. Introduction
in this article, You'll learn how to scan multiple packages in spring boot application with @ComponentScan annotation.All the classes and sub-packages will be scanned automatically under Spring Boot main class package. During the scan, it will detect @Component, @Configurations, @Bean annotated classes, and methods.
If you have many packages or paths in your application and all of them are outside the spring boot main class will not be scanned automatically.
2. Adding Single Package Package to @ComponentScan in Spring Boot
Creating a new class EmployeeOne in "com.javaprogramto.packagee.one". This is not under the main boot application.
How to Scan Multiple Packages with @ComponentScan Annotation
package com.javaprogramto.packagee.one;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class EmployeeOne {
private int id;
private String name;
}
By default, this class will not be scanned. So, you must tell to spring that scan this class.
To tell spring, use @ComponentScan with the argument that takes String value. as below
Note:
When you add @ComponentScan explicitly then-new package and current spring boot main application package will be scanned recursively.
package com.javaprogramto.springboot.SpringBootCofigurations;
import com.javaprogramto.packagee.one.EmployeeOne;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@ComponentScan("com.javaprogramto.packagee.one")
public class SpringBootCofigurationsApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(SpringBootCofigurationsApplication.class);
ApplicationContext context = app.run(args);
EmployeeOne e = (EmployeeOne)context.getBean("employeeOne");
System.out.println("Employee bean : "+e);
}
}
Output:
2020-05-08 20:47:23.666 INFO 7798 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-08 20:47:23.670 INFO 7798 --- [ main] j.s.S.SpringBootCofigurationsApplication : Started SpringBootCofigurationsApplication in 1.458 seconds (JVM running for 1.893)
Employee beab : com.javaprogramto.packagee.one.Employee@2fb69ff6
3. Scanning Multiple Packages in Spring Boot
Next, Create another class in the different packages as below.
package com.javaprogramto.packagee.two;
import org.springframework.stereotype.Component;
@Component
public class EmployeeTwo {
private int id;
private String name;
}
To add many packages to Component Scan, you should pass the String[] array to the @ComponentScan annotation.
package com.javaprogramto.springboot.SpringBootCofigurations;
import com.javaprogramto.packagee.one.EmployeeOne;
import com.javaprogramto.packagee.two.EmployeeTwo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@ComponentScan({"com.javaprogramto.packagee.one", "com.javaprogramto.packagee.two"})
public class SpringBootCofigurationsApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(SpringBootCofigurationsApplication.class);
Map<String, Object> customConfig = new HashMap<>();
customConfig.put("server.port", "9009");
ApplicationContext context = app.run(args);
EmployeeOne e = (EmployeeOne)context.getBean("employeeOne");
System.out.println("EmployeeOne bean : "+e);
EmployeeTwo e2 = (EmployeeTwo) context.getBean("employeeTwo");
System.out.println("EmployeeTwo bean : "+e2);
}
}
Output:
EmployeeOne bean : com.javaprogramto.packagee.one.EmployeeOne@41c89d2f
EmployeeTwo bean : com.javaprogramto.packagee.two.EmployeeTwo@410e94e
4. Scan with @ComponentScan with basePackages Attribute
There is another way to define the package scanning with basePackages attribute in @ComponentScan annotation.
You need not add the @ComponetScan annotation on the main class. Define this annotation in any class.
@ComponentScan(basePackages = "com.javaprogramto.packagee.three")
@Component
public class EmployeeTwo {
private int id;
private String name;
}
Next, create a new class in package three as below.
package com.javaprogramto.packagee.three;
import org.springframework.stereotype.Component;
@Component
public class EmployeeThree {
}
Now, run the main class with the below code.
EmployeeThree e3 = (EmployeeThree) context.getBean("employeeThree");
System.out.println("EmployeeThree bean : "+e3);
Output:
[EmployeeThree bean : com.javaprogramto.packagee.three.EmployeeThree@c7a977f]
5. Conclusion
In this article, you've seen how to add multiple packages to be scanned by the Spring Boot applications with @ComponentScan annotation with arguments and with basePackages attribute.
All the code is shown in this article is over GitHub.
You can download the project directly and can run in your local without any errors.
If you have any queries please post in the comment section.
0 Comments