Spring Boot Project Structure: Package Scanning, Auto-configuration, and Tests
When a Spring Boot project grows, check three things first: where the main application class sits, how to inspect auto-configuration, and how much of the application context each test should start. Avoid Java's default package and place the main class in the root package so component and entity scanning stay predictable. Auto-configuration supplies defaults based on dependencies, but verify what actually happened with the condition report and tests.
When a Spring Boot project grows, check three things first: where the main application class sits, how to inspect auto-configuration, and how much of the application context each test should start. Avoid Java's default package and place the main class in the root package so component and entity scanning stay predictable. Auto-configuration supplies defaults based on dependencies, but verify what actually happened with the condition report and tests.
This guide follows Spring Boot 4.1.2 documentation and covers package layout, diagnosing auto-configuration, environment-specific settings, and test scope. Adapt example names and dependencies to your project.
Put the main class in the root package
Spring Boot does not require a fixed directory layout. Avoid classes without a package declaration: scanning from the default package can inspect a much broader classpath and pick up unintended classes. Use a reverse-domain package and put the main class above feature packages, for example com.example.shop.ShopApplication with catalog and order subpackages.
The package of the class annotated with @SpringBootApplication is the default search boundary. Keep components, JPA entities, and Spring Data repositories beneath it, or configure scanning deliberately when they must live elsewhere.
Diagnose auto-configuration
@SpringBootApplication enables auto-configuration. Spring Boot considers classpath libraries and existing beans; for example, a database library may lead to a default DataSource when no application DataSource bean exists. A user bean can cause part of the default configuration to back off.
If expected configuration is missing, check the runtime classpath, the condition's required beans or properties, and whether package scanning includes the target. Run with --debug in development to see the condition report, which shows why candidates were applied or skipped. Exclude an auto-configuration only after confirming a conflict or intentional replacement; an arbitrary exclusion may remove required setup.
Keep environment settings outside code
Spring Boot reads properties or YAML, environment variables, system properties, and command-line arguments. Higher-precedence sources can override lower ones. You can keep shared values in application.properties and differences in application-dev.properties, then activate a profile with --spring.profiles.active=dev. Never commit passwords or tokens; supply secrets through the deployment secret store or environment.
Use one configuration-file format consistently. Use @ConfigurationProperties to bind related values into a typed object, or @Value for a simple individual value. When a value is unexpected, check active profiles, command-line arguments, environment variables, and external files in precedence order.
Choose a test context that matches the question
Pure logic tests do not need a Spring context. Use a slice test for a focused framework area and @SpringBootTest when the full integration path matters. Its default MOCK web environment loads a web context without starting an embedded server. RANDOM_PORT starts a real server on a random port and checks a wider HTTP path, with higher startup cost and more need to isolate external services.
In a random-port test, the server request may run on a different thread and transaction from the test method. Do not assume the test method's @Transactional rollback will undo changes made by the server. State which beans and server are started and how databases or brokers are isolated.
Troubleshooting order
If a bean or entity is missing, inspect package boundaries. If auto-configuration differs from expectation, inspect dependencies, the condition report, user beans, and properties. If local and deployed values differ, check profiles and source precedence. If integration tests are slow, consider unit or slice tests. If data remains after a test, check server-side transaction boundaries and cleanup.
Useful Spring Boot defaults depend on explicit choices about package boundaries, setting sources, and test scope. Keep the root package simple, use the condition report to see what Boot selected, and start a full context or server only where the test needs it.
References: Spring Boot 4.1.2 official documentation on code structure, auto-configuration, externalized configuration, profiles, and testing.
References
[Spring Boot 4.1.2: Structuring Your Code](https://docs.spring.io/spring-boot/reference/using/structuring-your-code.html)
[Spring Boot 4.1.2: Auto-configuration](https://docs.spring.io/spring-boot/reference/using/auto-configuration.html)
[Spring Boot 4.1.2: Externalized Configuration](https://docs.spring.io/spring-boot/reference/features/external-config.html)
[Spring Boot 4.1.2: Profiles](https://docs.spring.io/spring-boot/reference/features/profiles.html)
[Spring Boot 4.1.2: Testing Spring Boot Applications](https://docs.spring.io/spring-boot/reference/testing/spring-boot-applications.html)