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

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

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.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
spring ,java ,spring boot

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}