SpringBoot项目创建及入门探究
快速创建项目
使用IDEA的Spring Initializr快速创建项目
选定相关选择后,IDEA会联网快速建立项目。
简介
SpringBoot来简化Spring应用开发,约定大于配置,去繁从简,just run 就能创建一个独立的产品级别的应用
简化Spring应用开发的一个框架
整个Spring技术栈的一个大整合
J2EE开发的一站式解决方案
背景
J2EE笨重的开发、繁多的配置、低下的开发效率、复杂的部署流程、第三方技术集成难度大。
解决的问题
“Spring全家桶”时代。
SpringBoot -->J2EE一站式解决方案
SpringCloud–>分布式整体解决方案
优点
- 快速创建独立运行的Spring项目以及与主流框架集成
- 使用嵌入式的Servlet容器,应用无需打包成WAR包
- starters自动依赖与版本控制
- 大量的自动配置,简化开发,也可修改默认值
- 无需配置XML,无代码生成,开箱即用
- 准生产环境的运行时应用监控
- 与云计算的天然集成
入门
POM文件
打包
在pom.xml中加入插件代码
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
加载完插件后,点击右侧maven图标,打开LIfeCycle,双击package,等待程序运行完后,会提示打包后的文件位置,即target目录下,找到对应的jar包即为打包后的程序。
左下角的图标可以弹出右侧maven的菜单栏。
父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- spring-boot-starter-parent源码内的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<!-- spring-boot-dependencies源码内的properties定义了每一个依赖的版本-->
所以parent是SpringBoot的版本仲裁中心;
以后导入依赖默认是不需要写版本的。
导入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-web源码内包含如下依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.3.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
spring-boot-starter-web:
场景启动器,帮助我们导入了web模块正常运行所依赖的组件。
SpringBoot将所有的功能场景都抽取除了,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。
可以看到一系列启动器,如果要使用aop的功能,即可导入aop的启动器,如果要使用jdbc的功能,即可导入jdbc的启动器等等
入口类
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
@SpringBootApplication
SpringBoot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main类方法来启动SpringBoot应用。
进入@SpringBootApplication源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@SpringBootApplication包含的注解有:
四个元注解
Target、Retention、Documented、Inherited
@SpringBootConfiguration
SpringBoot的配置类
@SpringBootConfiguration标注在某个类上,表示这是一个SpringBoot的配置类;
@SpringBootConfiguration源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
包含了@Configuration:这个注解用来标注该类是一个配置类
配置类内的配置与配置文件一一对应,同时,
配置类也是容器中的一个组件,@Configuration源码内包含@Component注解。
@EnableAutoConfiguration
开启自动配置功能,以前我们需要配置的东西,SpringBoot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效。
@EnableAutoConfiguration源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@AutoConfigurationPackage
自动配置包,@AutoConfigurationPackage源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
}
内含@Import({Registrar.class})
Spring的底层注解@Import,给容器导入一个组件;这里导入的组件为Registrar.class;
Registrar.class的源码
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
Registrar() {
}
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
}
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata));
}
}
Registrar.class将主配置类(@SpringBootApplication标注的类)所在包下面所有子包里面的所有组件扫描到Spring容器中;
如图中的项目,Registrar.class会将SpringbootApplication所在包(com.baro.springboot)下的所有子包(pojo)的所有组件扫描到Spring容器中,前提是所有要扫描的包必须有@Component、@Controller、@Service、@ResponseBody等四个注解
@Import({AutoConfigurationImportSelector.class})
AutoConfigurationImportSelector.class:自动配置导包选择器,
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
这些组件会给容器中导入非常多的自动配置类,就是给容器中导入场景中需要的所有组件,并配置好这些组件。
SringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作。