1.什么是springboot
SpringBoot是Spring项目中的一个子工程,其实人们把Spring Boot 称为搭建程序的脚手架。其最主要作用就是帮我们快速的构建庞大的spring项目,并且尽可能的减少一切xml配置,做到开箱即用,迅速上手,让我们关注与业务而非配置。
2.为什么要用springboot
Spring Boot 优点非常多,如:
- 独立运行 Spring Boot而且内嵌了各种servlet容器,Tomcat、Jetty等,现在不再需要打成war包部署到容器中,Spring Boot只要打成一个可执行的 jar包就能独立运行,所有的依赖包都在一个jar包内。
- 简化配置 spring-boot-starter-web启动器自动依赖其他组件,简少了maven的配置。
- 自动配置 Spring Boot能根据当前类路径下的类、jar包来自动配置bean,如添加一个spring-boot-starter-web启动器就能拥有web的功能,无需其他配置。
- 无代码生成和XML配置 Spring Boot配置过程中无代码生成,也无需XML配置文件就能完成所有配置工作,这一切都是借助于条件注解完成的,这也是Spring4.x的核心功能之一。
- 应用监控 Spring Boot提供一系列端点可以监控服务及应用,做健康检测
3.Sprinboot有哪些优点
- 减少开发,测试时间和努力。
- 使用 JavaConfig 有助于避免使用 XML。
- 避免大量的 Maven 导入和各种版本冲突。
- 通过提供默认值快速开始开发。
- 没有单独的 Web 服务器需要。这意味着你不再需要启动 Tomcat,Glassfish或其他任何东西。
- 需要更少的配置 因为没有 web.xml 文件。只需添加用@ Configuration 注释的类,然后添加用@Bean 注释的方法,Spring 将自动加载对象并像以前一样对其进行管理。您甚至可以将@Autowired 添加到 bean 方法中,以使 Spring 自动装入需要的依赖关系中。
基于环境的配置 使用这些属性,您可以将您正在使用的环境传递到应用程序:-Dspring.profiles.active = {enviornment}。在加载主应用程序属性文件后,Spring 将在(application{environment} .properties)中加载后续的应用程序属性文件。
application.yml
spring.active.profile=dev
application-dev.yml
application-test.yml
application-prod.yml
4.Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?
启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:
@SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。
@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能:@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。 @ComponentScan:Spring组件扫描,从当前类所在的包以及子包扫描,之外的包扫描不到,所以我们在开发的时候,所有的类都在主类的子包下
5.springboot项目有哪几种运行方式
打包用命令或者放到容器中运行
用 Maven/Gradle 插件运行
直接执行 main 方法运行
6.如何理解springboot中的starters?
Starters可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,你可以一站式集成Spring及其他技术,而不需要到处找示例代码和依赖包。如你想使用Spring JPA访问数据库,只要加入springboot-starter-data-jpa启动器依赖就能使用了。Starters包含了许多项目中需要用到的依赖,它们能快速持续的运行,都是一系列得到支持的管理传递性依赖。
7.springboot自动配置原理
这个就得从springboot项目的核心注解@SpringbootApplication说起了,这个注解包含了三个注解,其中一个是@EnableAutoConfiguration注解,这个注解主要是开启自动配置的,这个注解会"猜"你将如何配置 spring,前提是你已经添加 了 jar 依赖项,比如项目中引入了 spring-boot-starter-web ,这个包里已经添加 Tomcat 和 SpringMVC,这个注解就会自动假设您在开发一个 web 应用程序并添加相应的 spring 配置,springboot默认有一个spring-boot-autoconfigure包,大多数常用的第三方的配置都自动集成了,像redis、es等,这里边有一个META-INF/spring.factories文件,这里边定义了所有需要加载的bean的全路径,spring会根据反射的原理,创建这些对象,放到IOC容器中,加载时需要的参数,通过JavaConfig的方式加载配置文件中的参数然后创建了对应的对象,这就是自动配置的原理
8、springboot在启动之前就要加载某些类用到哪些注解
在 Spring Boot 中,如果你需要在应用启动之前加载某些类或者执行一些特定的初始化逻辑,可以使用以下几种注解和接口:
-
@PostConstruct:
- 用于在 bean 的初始化之后执行一些逻辑。
- 适用于特定 bean 的初始化逻辑。
@Component public class MyInitializer { @PostConstruct public void init() { // Initialization logic System.out.println("MyInitializer is initialized"); } }
-
CommandLineRunner 和 ApplicationRunner:
- 用于在 Spring Boot 应用启动完成后执行一些逻辑。
- 可以通过实现
CommandLineRunner
或ApplicationRunner
接口来实现。
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // Initialization logic System.out.println("MyCommandLineRunner is running"); } }
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationArguments; import org.springframework.stereotype.Component; @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // Initialization logic System.out.println("MyApplicationRunner is running"); } }
-
@Bean(initMethod = “init”):
- 用于在 Spring 管理的 bean 初始化时执行某个方法。
- 在配置类中声明。
@Configuration public class AppConfig { @Bean(initMethod = "init") public MyService myService() { return new MyService(); } } public class MyService { public void init() { // Initialization logic System.out.println("MyService is initialized"); } }
-
@EventListener(ApplicationReadyEvent.class):
- 用于在应用完全启动并且 Spring Context 已经准备好后执行一些逻辑。
import org.springframework.context.event.EventListener; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.stereotype.Component; @Component public class MyEventListener { @EventListener(ApplicationReadyEvent.class) public void onApplicationReady() { // Initialization logic System.out.println("Application is ready"); } }
-
@DependsOn:
- 指定 bean 加载顺序,确保某些 bean 在其他 bean 之前加载。
@Component @DependsOn("anotherBean") public class MyInitializer { @PostConstruct public void init() { // Initialization logic System.out.println("MyInitializer is initialized"); } }
总结
- 使用
@PostConstruct
进行 bean 初始化后的逻辑执行。 - 使用
CommandLineRunner
或ApplicationRunner
在应用启动完成后执行逻辑。 - 使用
@Bean(initMethod = "init")
在 bean 初始化时执行特定方法。 - 使用
@EventListener(ApplicationReadyEvent.class)
在应用完全启动后执行逻辑。 - 使用
@DependsOn
确保特定 bean 的加载顺序。
这些方法和注解可以帮助你在 Spring Boot 应用启动之前或者启动之后执行一些初始化逻辑,根据实际需求选择合适的方式。