Spring Boot Default App Structure and Component Scan

DZone 's Guide to

Spring Boot Default App Structure and Component Scan

The Spring Boot application annontation causes many of your subpackages to be scanned by convention.

· Java Zone ·
Free Resource

I’m familiar with the Spring Framework’s concept of a ‘component scan’ to find its managed beans when the app initializes, but I was looking for how to configure the package(s) for the component scan in a Spring Boot app.

Turns out (see docs here), by convention if your main app class is annotated with @SpringBootApplication and is in a package with all your code in packages below this point, then those subpackages are scanned by default.

Here’s a simple Spring Boot app class:

package kh.simplespringboot;

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

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

All packages below kh.simplespringboot in the example by default will be scanned for Spring components.

java, spring, spring boot

Published at DZone with permission of Kevin Hooke , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}