Understanding the Three Core Annotations of Spring Boot: @Configuration, @ComponentScan, and @EnableAutoConfiguration

This tutorial explains Spring Boot's three core annotations—@Configuration, @ComponentScan, and @EnableAutoConfiguration—by showing how Java Config replaces XML, demonstrating bean creation, lifecycle management, component scanning, and auto‑configuration with practical code examples for a Spring application.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding the Three Core Annotations of Spring Boot: @Configuration, @ComponentScan, and @EnableAutoConfiguration

Preface

Spring Boot's main feature is zero XML configuration, enabling auto‑configuration and fully automated jar packaging. It is the core of micro‑services, and Spring Cloud builds on it. The article introduces the three most essential Spring Boot annotations.

Configuration

Since Spring 4, Java Config is recommended to replace application.xml. In Spring Boot, Java Config completely eliminates XML configuration. Below is an example.

Example

Create a bean class:

public class SomeBean {
    public void doWork() {
        System.out.println("do work...");
    }
}

Then create a configuration class:

@Configuration
public class Config {
    @Bean
    public SomeBean someBean() {
        return new SomeBean();
    }
}

The @Configuration annotation marks the class as a Spring configuration, and @Bean registers the returned object as a bean in the container.

Simple Test

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        SomeBean sb = context.getBean(SomeBean.class);
        sb.doWork();
    }
}

Running this prints:

do work...

Extension

A complete bean may define id, class, initMethod, destroyMethod, scope, etc. The example adds init and destroy methods.

public class SomeBean {

    private void init() {
        System.out.println("init...");
    }

    public void doWork() {
        System.out.println("do work...");
    }

    private void destroy() {
        System.out.println("destroy...");
    }

}

Update the configuration to specify lifecycle methods:

@Configuration
public class Config {

    @Bean(initMethod = "init", destroyMethod = "destroy")
    public SomeBean someBean() {
        return new SomeBean();
    }
}

Test class demonstrates bean lifecycle:

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        SomeBean sb1 = context.getBean(SomeBean.class);
        System.out.println(sb1);

        SomeBean sb2 = context.getBean(SomeBean.class);
        System.out.println(sb2);
        context.close();
    }
}

Output:

init...
com.spring.SomeBean@16022d9d
com.spring.SomeBean@16022d9d
destroy...

This completes the configuration lifecycle.

@ComponentScan

@ComponentScan defines the packages to scan for components such as @Controller, @Service, @Component, and @Repository, equivalent to the XML element <context:component-scan base-package="..."/>.

Basic Usage

Common attributes include basePackages/value (scan path), basePackageClasses (specific classes), includeFilters, and excludeFilters. Filter types can be ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, REGEX, or CUSTOM.

@ComponentScan(value="com.maple.learn",
    excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)},
    includeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class})}
)
public class SampleClass{
    …
}

@EnableAutoConfiguration

@EnableAutoConfiguration is a meta‑annotation that imports auto‑configuration classes. Its source includes @Import(AutoConfigurationImportSelector.class), which loads all matching @Configuration classes into the IOC container via SpringFactoriesLoader reading META‑INF/spring.factories.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};

}

About the Author

The author signs off and invites readers to the next article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentConfigurationSpring Bootannotations
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.