Skip to main content

Avoid Using @SpringBootApplication and @ComponentScan in the Default Package

Medium
maintainabilityperformancebest practices

What is it?

This practice warns against using @SpringBootApplication and @ComponentScan annotations in Java classes that belong to the default package.

Why apply it?

The default package automatically results in scanning the entire classpath, significantly slowing application startup and potentially causing BeanDefinitionStoreException due to scanning inappropriate packages, including the Spring Framework itself.

How to fix it?

Always place your main Spring Boot application class and any class annotated with @ComponentScan into a named package. This ensures more efficient and targeted scanning of your application's beans.

Examples

Example 1:

Negative

This negative example places @SpringBootApplication in the default package, causing inefficient scanning and potential startup failure.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // Noncompliant
public class DefaultPackageApp {
public static void main(String[] args) {
SpringApplication.run(DefaultPackageApp.class, args);
}
}

Example 2:

Positive

In this positive example, @SpringBootApplication is correctly used in a named package, ensuring efficient scanning and preventing startup issues.

package com.example.bootapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // Compliant
public class RootBootApp {
public static void main(String[] args) {
SpringApplication.run(RootBootApp.class, args);
}
}

Negative

The following negative example demonstrates @ComponentScan used in the default package without any base configuration, causing scanning of the entire classpath.

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("") // Noncompliant
public class DefaultConfig {
}

Example 3:

Positive

In this positive example, @ComponentScan is used correctly within a named package with a defined base package, avoiding scanning issues.

package com.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example.service") // Compliant
public class AppConfig {
}