Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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;

@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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
spring ,java ,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 }}