{"dataset":{"id":"00000000-0000-0000-0000-000000000052","slug":"spring-boot-best-practices","name":"Spring Boot best practices","description":"Source-linked guidance on Spring Boot fundamentals: code structure, dependency injection, auto-configuration, externalized configuration, and a first application. Scoped to current Spring Boot.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"Building a first application","sourceUrl":"https://docs.spring.io/spring-boot/tutorial/first-application/index.html","text":"Utilize `start.spring.io` to generate project structures, selecting the \"Web\" starter for immediate coding. Configure the `spring-boot-starter-webmvc` dependency in `build.gradle` to automatically include Tomcat and Spring MVC. Establish the standard source directory `src/main/java`, creating packages like `com.example` for files such as `MyApplication.java`. Annotate the main class with `@SpringBootApplication`, a meta-annotation combining `@SpringBootConfiguration`, `@EnableAutoConfiguration`, and `@ComponentScan`. Use `@EnableAutoConfiguration` to allow Spring Boot to guess configuration based on added JAR dependencies. Implement `@RestController` as a stereotype annotation for web controllers and `@RequestMapping` to map HTTP requests, such as the `/` path, to specific methods. The `@RestController` ensures resulting strings render directly to callers. Run the application using `SpringApplication.run(MyApplication.class, args)` in Java or `runApplication<MyApplication>(*args)` in Kotlin. Scope: Spring Boot. Reviewed October 4, 2026.","id":"spring-boot-best-practices:Building a first application","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Externalized configuration","sourceUrl":"https://docs.spring.io/spring-boot/reference/features/external-config.html","text":"Externalize configuration to keep identical application code across diverse environments, using Java properties files, YAML, environment variables, and command-line arguments. Inject values into beans via @Value, access them through the Environment abstraction, or bind structured objects using @ConfigurationProperties. Spring Boot orders property sources so later ones override earlier, running from default properties through SPRING_APPLICATION_JSON up to command-line arguments. Config data priority puts jar-packaged profile-specific properties above jar-packaged base files, and external profile-specific files above external base files. Stick to one format; if .properties and YAML share a location, .properties takes precedence. @PropertySource properties load only at context refresh, too late for early-read properties like logging.* and spring.main.*. Use underscores for environment variable keys when periods are disallowed. Scope: Spring Boot. Reviewed October 4, 2026.","id":"spring-boot-best-practices:Externalized configuration","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Auto-configuration","sourceUrl":"https://docs.spring.io/spring-boot/reference/using/auto-configuration.html","text":"Spring Boot auto-configuration automatically sets up your application based on added jar dependencies, such as configuring an in-memory HSQLDB if present and unconfigured. Opt-in via @EnableAutoConfiguration or @SpringBootApplication annotations on a @Configuration class, adding only one to your primary configuration. This process is non-invasive; defining custom beans like DataSource causes default embedded database support to recede. To diagnose applied auto-configuration, start with the --debug switch, enabling debug logs and printing a conditions report to the console. To disable unwanted classes, use the exclude attribute on annotations or the spring.autoconfigure.exclude property. If a class is absent from the classpath, use the excludeName attribute with its fully qualified name. Both @SpringBootApplication and @EnableAutoConfiguration support these exclusions. Note that while auto-configuration classes are public, only the class name constitutes a public API aspect. Scope: Spring Boot. Reviewed October 4, 2026.","id":"spring-boot-best-practices:Auto-configuration","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Spring beans and dependency injection","sourceUrl":"https://docs.spring.io/spring-boot/reference/using/spring-beans-and-dependency-injection.html","text":"Use standard Spring Framework techniques to define beans and inject dependencies. Constructor injection is generally recommended for wiring dependencies, while @ComponentScan locates beans. When your application class resides in a top package, @ComponentScan requires no arguments, or you may use @SpringBootApplication, which implicitly includes it. All application components, including @Component, @Service, @Repository, and @Controller, are automatically registered as Spring Beans. For beans with multiple constructors, mark the desired constructor with @Autowired. Constructor injection allows dependency fields to be marked as final, ensuring they cannot be subsequently changed after instantiation. Scope: Spring Boot. Reviewed October 4, 2026.","id":"spring-boot-best-practices:Spring beans and dependency injection","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Structuring code and the main application class","sourceUrl":"https://docs.spring.io/spring-boot/reference/using/structuring-your-code.html","text":"Spring Boot imposes no specific code layout requirements, but adhering to best practices aids development. Avoid the default package, as it causes issues with @ComponentScan, @ConfigurationPropertiesScan, @EntityScan, and @SpringBootApplication by reading every class from every jar. Instead, follow Java’s recommended naming conventions using a reversed domain name, such as com.example.project. Place the main application class in a root package above other classes to define a base search package for items like JPA @Entity classes. This structure ensures component scan applies only to the project. While @SpringBootApplication is typically used on the main class, @EnableAutoConfiguration and @ComponentScan can be used instead. For domain-based structures, consider Spring Modulith. The recommended layout groups classes under the root package, with the main class containing the main method and @SpringBootApplication annotation. Scope: Spring Boot. Reviewed October 4, 2026.","id":"spring-boot-best-practices:Structuring code and the main application class","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}