SpringBoot(一)HelloWorld探究

1.@SpringbootApplication注解

Spring Boot默认是扫描@SpringBootApplication注解的类(即启动类)的同包以及子包下的类。

使用@SpringbootApplication注解 可以解决根类或者配置类头上注解过多的问题,一个@SpringbootApplication相当于@Configuration,@EnableAutoConfiguration@ComponentScan 并具有他们的默认属性值

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
  • @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.


2.@EnableAutoConfiguration注解

@EnableAutoConfiguration注解的作用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,这就减少了开发人员的工作量。

The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.


Starters and Auto-Configuration
Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick-and-choose jar dependencies outside of the starters and Spring Boot will still do its best to auto-configure your application.

第二个类级别注解是@EnableAutoConfiguration。这个注解告诉Spring Boot“猜测”将如何配置Spring,它是基于添加的jar依赖。 由于spring-boot-starter-web添加了Tomcat和Spring MVC,因此自动配置将假设正在开发一个Web应用程序并相应地设置Spring。
启动器和自动配置(Starters & Auto-Configuration):
自动配置旨在与“Starters”配合使用,但这两个概念不直接绑定。可以自由选择和选择起始者以外的jar依赖,Spring Boot仍将尽力自动配置应用程序。


3.main()方法

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.

“main”方法应用程序的最后一部分是主(main)方法。 这只是一个遵循Java约定的应用程序入口点的标准方法。main方法通过调用run来委托Spring Boot SpringApplication类。SpringApplication将引导应用程序,启动Spring,从而启动自动配置Tomcat Web服务器。需要传递Example.class作为run方法的参数来告诉SpringApplication,这是主要的Spring组件。args数组也被传递以暴露任何命令行参数。


4.继承 spring-boot-starter-parent

作用是Spring-Boot为我们自动添加相关的依赖,因为spring-boot-starter-parent它的父项目是spring-boot-dependencies

spring-boot-dependencies项目中 springboot 为我们自动管理了一些依赖的jar包的版本:

如果不想使用Spring Boot中的默认版本,可以在pom.xml文件的properites标签中指定我们自己想要引入的版本,这样就可以覆盖默认的版本。
比如想使用不同版本的Spring Data,具体如下:

1
2
3
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

在properties标签中还可以指定JDK编译的版本和项目的编码格式:

1
2
3
4
5
6
7
<properties>
<!-- 指定项目编码为 UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 使用 java 1.8 -->
<java.version>1.8</java.version>
</properties>

如果不是在spring-boot-dependencies中管理的依赖,那自然需要我们制定依赖的版本了。

5.启动器(starter)

在HelloWorld示例中,用到了web的starter。

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

官网上对于 starter 的描述:

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.


The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

starter 其实就是一系列依赖的组合,比如spring-boot-starter-web这个starter里就包含了tomcat、sping-web、springwebmvc等这些依赖。通过这些依赖,一个starter就可以完成一个主要的功能。比如需要用aop,那就导入aop的starter,需要用的消息队里,那就导入消息队列比如activemq的starter,要使用redis那就导入redis的starter。这对于实际的开发非常方便。

总结:Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来,版本由 SpringBoot 自动控制。要用什么功能就导入什么场景的启动器。

6.spring-boot-maven-plugin 插件

可以为SpringBoog项目提供Maven的操作方式,Spring Boot Maven plugin的5个Goals

  • spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war,同时保留mvn package生成的jar/war为.origin
  • spring-boot:run,运行Spring Boot应用
  • spring-boot:start,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
  • spring-boot:stop,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
  • spring-boot:build-info,生成Actuator使用的构建信息文件build-info.properties

这里介绍一下 spring-boot:repackage 和 spring-boot:run 这两个。
spring-boot:repackage :可以将项目打包成fat jar(executable jar)后,命令是:
mvm package spring-boot:repackage,或者直接使用 mvm package,(如果使用 mvn spring-boot:repackage 则会报错)。之后我们就可以直接 通过 java -jar 的命令来启动这个 SpringBoot 项目(试了一下即使没有配置 mainClass 也是成功的)

mvn spring-boot:run:在 pom.xml 文件所在的目录下运行该命令即可启动 SpringBoot 项目,和在 Application.java 中运行main()方法的结果是一样的

The Spring Boot Maven plugin provides many convenient features:

  • It collects all the jars on the classpath and builds a single, runnable “über-jar”, which makes it more convenient to execute and transport your service.
  • It searches for the public static void main() method to flag as a runnable class.
  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
------ 本文完 ------