springboot使用application启动类
时间: 2023-09-05 19:07:47 浏览: 57
Spring Boot 提供了一种方便的方式来启动应用程序,即通过一个带有 `main()` 方法的启动类。这个类通常被称为 `Application` 类。
在 Spring Boot 应用程序中,使用 `@SpringBootApplication` 注解标记主类。这个注解是一个复合注解,包括 `@EnableAutoConfiguration`、`@ComponentScan` 和 `@Configuration`。
下面是一个简单的示例:
```java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
`SpringApplication.run()` 方法接收两个参数:第一个参数是应用程序主类的引用,第二个参数是命令行参数。这个方法会启动 Spring Boot 应用程序,并将控制权交给 Spring 框架。
在 Spring Boot 应用程序中,可以使用 `application.properties` 或 `application.yml` 文件来配置应用程序属性。这些文件应该放在应用程序的 classpath 下的 `/src/main/resources` 目录中。
通过使用 `SpringApplication.setDefaultProperties()` 方法,可以设置默认属性,这些属性会覆盖 `application.properties` 或 `application.yml` 文件中的属性。例如:
```java
SpringApplication.setDefaultProperties(Collections.singletonMap("server.port", "8080"));
```
这会将默认端口设置为 8080。
希望这可以帮助你了解如何使用 `Application` 类在 Spring Boot 应用程序中启动应用程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)