> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iii.-using-spring-boot/14.-structuring-your-code/14.2.-locating-the-main-application-class.md).

# 14.2. 放置应用的main类

通常建议将应用的main类放到其他类所在包的顶层(root package)，并将`@EnableAutoConfiguration`注解到你的main类上，这样就隐式地定义了一个基础的包搜索路径（search package），以搜索某些特定的注解实体（比如@Service，@Component等） 。例如，如果你正在编写一个JPA应用，Spring将搜索`@EnableAutoConfiguration`注解的类所在包下的`@Entity`实体。

采用root package方式，你就可以使用`@ComponentScan`注解而不需要指定`basePackage`属性，也可以使用`@SpringBootApplication`注解，只要将main类放到root package中。

下面是一个典型的结构：

```
com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java
```

`Application.java`将声明`main`方法，还有基本的`@Configuration`。

```java
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
```
