SpingBoot学习(一)

一、概述

  Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。

简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题---习惯大于约定。

   Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。

二、使用

  1、Spring Boot提供了一系列的依赖包,所以需要构建工具的支持:maven 或 gradle。

  2、pom.xml  

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.larry.spring</groupId>
    <artifactId>larry-spring-demo4</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

  说明:   通常让你的Maven POM文件继承 spring-boot-starter-parent,并声明一个或多个 Starter POMs依赖即可。

  1、那spring-boot-starter-parent是什么?

  原文:https://blog.csdn.net/qq_35981283/article/details/77802771 

  Maven的用户可以通过继承spring-boot-starter-parent项目来获得一些合理的默认配置。这个parent提供了以下特性:

  (1)默认使用Java 8
  (2)使用UTF-8编码
  (3)一个引用管理的功能,在dependencies里的部分配置可以不用填写version信息,这些version信息会从spring-boot-dependencies里得到继承。
  (4)识别过来资源过滤(Sensible resource filtering.)
  (5)识别插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  (6)能够识别application.properties和application.yml类型的文件,同时也能支持profile-specific类型的文件(如: application-foo.properties and application-foo.yml,这个功能可以更好的配置不同生产环境下的配置文件)。
  (7)maven把默认的占位符${…​}改为了@..@

   2、starter模块,简单的说,就是一系列的依赖包组合。

     spring-boot-starter-web  包含了很多内容,spring-webmvc、spring-web、jackson、validation、tomcat、starter。

 基本上,如果没有特别的需要,现在就可以直接写Controller了!!! 

package cn.larry.spring.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

  说明:这里有两个新东西:@EnableAutoConfiguration 和 SpringApplication 。

  @EnableAutoConfiguration 用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。

  在上面这个例子中,@EnableAutoConfiguration 会判断出这是一个web应用,所以会创建相应的web环境。

   SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤:

  1. 创建一个合适的ApplicationContext实例 (取决于classpath)。
  2. 注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。
  3. 刷新application context,加载所有单例beans。
  4. 激活所有CommandLineRunner beans。

  现在,直接右键启动main方法即可。

 原文:http://www.cnblogs.com/larryzeal/p/5765945.html

猜你喜欢

转载自www.cnblogs.com/dongtian-blogs/p/10831222.html