[[using.structuring-your-code]]
= Structuring Your Code

Spring Boot does not require any specific code layout to work.
However, there are some best practices that help.

TIP: If you wish to enforce a structure based on domains, take a look at https://spring.io/projects/spring-modulith#overview[Spring Modulith].



[[using.structuring-your-code.using-the-default-package]]
== Using the "`default`" Package

When a class does not include a `package` declaration, it is considered to be in the "`default package`".
The use of the "`default package`" is generally discouraged and should be avoided.
It can cause particular problems for Spring Boot applications that use the javadoc:org.springframework.context.annotation.ComponentScan[format=annotation], javadoc:org.springframework.boot.context.properties.ConfigurationPropertiesScan[format=annotation], javadoc:org.springframework.boot.autoconfigure.domain.EntityScan[format=annotation], or javadoc:org.springframework.boot.autoconfigure.SpringBootApplication[format=annotation] annotations, since every class from every jar is read.

TIP: We recommend that you follow Java's recommended package naming conventions and use a reversed domain name (for example, `com.example.project`).



[[using.structuring-your-code.locating-the-main-class]]
== Locating the Main Application Class

We generally recommend that you locate your main application class in a root package above other classes.
The xref:using/using-the-springbootapplication-annotation.adoc[`@SpringBootApplication` annotation] is often placed on your main class, and it implicitly defines a base "`search package`" for certain items.
For example, if you are writing a JPA application, the package of the javadoc:org.springframework.boot.autoconfigure.SpringBootApplication[format=annotation] annotated class is used to search for javadoc:jakarta.persistence.Entity[format=annotation] items.
Using a root package also allows component scan to apply only on your project.

TIP: If you do not want to use javadoc:org.springframework.boot.autoconfigure.SpringBootApplication[format=annotation], the javadoc:org.springframework.boot.autoconfigure.EnableAutoConfiguration[format=annotation] and javadoc:org.springframework.context.annotation.ComponentScan[format=annotation] annotations that it imports defines that behavior so you can also use those instead.

The following listing shows a typical layout:

[source]
----
com
 +- example
     +- myapplication
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java
----

The `MyApplication.java` file would declare the `main` method, along with the basic javadoc:org.springframework.boot.autoconfigure.SpringBootApplication[format=annotation], as follows:

include-code::MyApplication[]
