1      spring boot入门

1.1  环境准备

JDK 7及以上

eclipse开发工具

项目管理工具Maven

本文采用Java 1.7.0_60Spring Boot 1.5.4.RELEASE(或1.5.2.RELEASE调试通过。

 

spring-boot相关项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git

1.2  Maven构建项目

方案一:从官网构建:http://start.spring.io/

wKiom1m_QGfDHcY9AADIfk3b3-8651.png

generate Project到本地:demo.zip

wKioL1m_UESCgn-lAAArrRHlIJA590.png

解压,importeclipse中即可。

方案二:手动创建

wKiom1m_UI3hijTfAAAZ5f8gR4s556.png

项目源码链接:

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git


  • pom.xml文件中导入依赖:

<parent>

<!-- 如果本地没有依赖包,去中央仓库下载

spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。-->

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>1.5.4.RELEASE</version>

   </parent>

<dependencies>

      <dependency>

        <!-- spring boot 引入Web模块 -->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

      <dependency>

        <!--  暂时用不上 spring-boot-starter:核心模块,包括自动配置支持、日志和YAML-->

          <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter</artifactId>

      </dependency>

      <dependency>

        <!-- spring-boot-starter-test:测试模块,包括JUnitHamcrestMockito-->

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-test</artifactId>

         <scope>test</scope>

      </dependency>

</dependencies>

 

   <build>

      <plugins>

        <plugin>

           <!-- 配置spring bootmaven插件 -->

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

      </plugins>

   </build>

 

  • 编写controller

   新建包:com.wyait.boot   com.wyait.boot.controller

   新建启动类:HelloApplication

/**

 *

 * @项目名称:springboot

 * @类名称:HelloApplication

 * @类描述:我的第一个springboot启动类

 * @创建人:wyait

 * @创建时间:2017626上午11:28:57

 * @version1.0

 */

@Configuration//这是一个配置Spring的配置类

@SpringBootApplication//@SpringBootApplicationSpring Boot项目的核心注解,主要目的是开启自动配置。

public class HelloApplication {

  

   publicstatic void main(String[] args) {

      //启动spring boot应用

      SpringApplication.run(HelloApplication.class,args);

   }

}

   新建HelloController

/**

 *

 * @项目名称:springboot

 * @类名称:HelloController

 * @类描述:第一个spring Boot Controller

 * @创建人:wyait

 * @创建时间:2017626上午11:35:19

 * @version

 */

@Controller

public class HelloController {

 

   @RequestMapping("/hello")

   @ResponseBody

   publicString hello() {

      return"hello spring boot!";

   }

}

   启动项目:

方式一:Run AsàJava Application

wKiom1m_UKrx37zdAAD2H4rzq7c120.png

方式二:选中项目—>Run As-->Maven Build…-->spring boot:run(不推荐使用。网上说是配置热部署之后,重复启动有端口占用问题)

wKiom1m_ULXRSYKYAAC7Ag-6UEY973.png

启动console效果:

wKioL1m_UJCyaq2hAAHktR7Pmb4519.png

   访问测试:http://127.0.0.1:8080/hello

wKioL1m_UJzQ8lKuAAA7Krb6Go0583.png

 

2      spring boot核心

下载Spring Boot源码

2.1  入口类和@SpringBootApplication

Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法。

 

  • @SpringBootApplication注解,是Spring Boot的核心注解,是一个组合注解。跟踪源码:

wKiom1m_UN3ysO_vAABhQha2u6c565.png

  • @SpringBootConfiguration也是组合注解:

wKioL1m_ULbidgVBAAAq2Kfuqdg662.png

  • @EnableAutoConfiguration:启动自动配置,该注解会使Spring Boot根据项目中配置的依赖,自动配置所需的依赖jar包:比如:我们添加了spring-boot-starter-web配置,Spring Boot会自动配置tomcatSpring MVC

wKiom1m_UPCy3wyFAAAz0CsuR6I718.png

  • @ComponentScan:默认扫描@SpringBootApplication所在类的同级目录和它的子目录(当前包以及它的子包)。

  • 关闭自动配置

查看spring-boot-autoconfigure的自动配置实现。。。。too much

wKiom1m_UPqzjCp4AAAzxU-EEyw579.png

在实际的开发中,我们可能不需要某一项进行自动配置。这时候如何设置?

 

比如:项目中不需要redis自动配置:找到自动配置中对应的后缀为:*AutoConfiguration.class文件

wKioL1m_UNySU3R8AABKW0ii1lA999.png

只需要在@SpringBootApplication注解后面添加(exclude={RedisAutoConfiguration.class}

wKioL1m_UO_B7hDbAABZeHwHpIY222.png

 

其他配置类似。

2.3  全局配置文件

Spring Boot项目使用全局的配置文件是:application.propertiesapplication.yml,在/resources目录下或者类路径下/config下,一般我们都放在/resources

  • 新建application.properties文件

wKiom1m_US3w2fLPAAAXzV067AI065.png

更多更详细的配置参考文件:application.propertiesSpring Bootapplication配置详解》  或参考官网http://projects.spring.io/spring-boot/

  • 添加设置端口配置:

  • 修改进入DispatcherServlet的规则为:*.html

wKioL1m_UXrzy6J-AAANo390o94983.png

重新启动,访问:

wKioL1m_UQayKWNEAAA6iKM-rm8664.png


 

2.4  starter pom相关依赖

wKiom1m_Udahw5FdAABYBctYIhQ822.png

wKiom1m_Ud_wRhIoAADW2i7YDqU685.png

wKioL1m_UbiRSDu7AAHUaYpOlqk513.png

 

wKioL1m_UcKwVv3EAAHUaYpOlqk779.png

wKiom1m_UfuASbNpAAFk1aybdcU309.png

 

2.5  Spring Boot自动配置原理

Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该文件中的配置载入到spring容器中。

跟踪源码:

wKiom1m_UgWwk8XeAAAeszXctNU802.png

找到SpringApplication的初始化方法:initialize();

wKiom1m_UiGyXXQXAAFqXs4Cd_c287.png

//获取spring工厂配置数据

setInitializers((Collection)getSpringFactoriesInstances(

                            ApplicationContextInitializer.class));

wKiom1m_UjHCuzvAAACE_GvSTJM012.png

//读取spring.factories中的元素集合数据

Set<String>names = new LinkedHashSet<String>(

                            SpringFactoriesLoader.loadFactoryNames(type,classLoader));

wKiom1m_UnajMuLkAADbKdOMvDE695.png


读取资源数据:

FACTORIES_RESOURCE_LOCATION= "META-INF/spring.factories";

wKioL1m_UhGQeO2xAAAe7ncjoX8660.png

META-INF/spring.factories文件在Spring-Boot-autoconfigura-1.5.4.RELEASE.jar包下面:

里面存放了所有自动配置启动类信息。

wKioL1m_UlizAt5JAAIiiOpVG6Q209.png

2.6  举例:redis自动配置

找到RedisAutoConfiguration,跟踪源码:

wKiom1m_Up6wR4jbAAEIi_fDENE684.png

@ConditionalOnClass({JedisConnection.class, RedisOperations.class, Jedis.class })意思是:如果同时存在这三个类,就自动配置redis,进行redis实例化。否则不进行实例化。

RedisProperties读取配置信息,进行redis实例化

wKioL1m_Un_w6eK6AAD1NQtof-E518.png

具体细节,可以跟踪源码查看。。。//TODO

2.7  条件注解

wKioL1m_UpKRfhGkAABL0fiVJnY330.png

wKiom1m_UszRg8P3AAD7w-vX8Oc421.png

项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址:https://github.com/wyait/spring-boot-1.5.4.git


spring boot系列文章:

spring boot 1.5.4 概述(一)

spring boot 1.5.4入门和原理(二)

spring boot 1.5.4 之web开发(三)

spring boot 1.5.4 整合JSP(四)

spring boot 1.5.4 集成devTools(五)

spring boot 1.5.4 集成JdbcTemplate(六)

spring boot 1.5.4 集成spring-Data-JPA(七)

spring boot 1.5.4 配置文件详解(八)

spring boot 1.5.4 统一异常处理(九)

spring boot 1.5.4 定时任务和异步调用(十)

spring boot 1.5.4 整合log4j2(十一)

spring boot 1.5.4 整合 mybatis(十二)

spring boot 1.5.4 整合 druid(十三)

spring boot 1.5.4 之监控Actuator(十四)

spring boot 1.5.4 整合webService(十五)

spring boot 1.5.4 整合redis、拦截器、过滤器、监听器、静态资源配置(十六)

spring boot 1.5.4 整合rabbitMQ(十七)

spring boot 1.5.4 集成Swagger2构建Restful API(十八)

spring boot 1.5.9 整合redis(十九