Spring Boot(一)

1.什么是Spring Boot?

  Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置(习惯由于配置),从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

  使用Spring Boot很容易创建一个独立运行(运行jar,内嵌Servlet容器)、准生产级别的应用Spring框架的项目。使用Spring Boot可以不用或只需要很少的Spring配置。

2.Spring Boot的优缺点?

  优点:

  (1)快速构建项目;

  (2)对主流开发框架的无配置集成;

  (3)项目可独立运行,无需外部依赖的Servlet容器;

  (4)提供运行时的应用监控;

  (5)极大的提高了开发、部署效率;

  (6)与云计算的天然集成。

  缺点:

  (1)书籍文档较少且不够深入;

  (2)如果你不认同Spring框架。

3.入门

  (1)设置spring boot的parent

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

  Spring boot的项目必须要将parent设置为spring bootparent,该parent包含了大量默认的配置,大大简化了我们的开发。

  (2)导入spring boot的web支持

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

  (3)添加Spring boot的插件

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

  (4)编写一个Spring Boot的应用

  a.先来看一下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>com.anker.test</groupId>
  <artifactId>anker-springboot</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.1.RELEASE</version>
  </parent>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
  </properties>
  <dependencies>
      <!-- 添加对web的支持 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
  </dependencies>
  <!-- 添加Springboot的插件 -->
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>
View Code

  b.编写一个简单的启动类

package com.anker.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@SpringBootApplication
@Configuration
//@RestController (@Controller + @ResponseBody)
public class HelloApplication {
    
    @RequestMapping("hello")
    @ResponseBody
    //@GetMapping("hello")相当于 @RequestMapping(value = "/***", method = RequestMethod.GET)
    public String hello() {
        return "hello world!";
    }
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloApplication.class, args);
    }

}
View Code

  简单的代码说明:

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

    2、@Configuration:这是一个配置Spring的配置类;

    3@Controller:标明这是一个SpringMVCController控制器;

    4main方法:在main方法中启动一个应用,即:这个应用的入口;

  c.启动

  第一种:

  

  第二种:

  

  d.结果

  

     这样一个简单的Spring boot项目就到此完成了。

  那么接下来,我们来聊一些细节的东西:

  1.jar包依赖关系

  我们只是在pom.xml配置了几个依赖结果,结果发现导入了那么多的jar包。

  

  2.Spring Boot的核心

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

    @SpringBootApplication注解是Spring Boot的核心注解,它其实是一个组合注解

   

该注解主要组合了以下注解:

  a.@SpringBootConfiguration:这是Spring Boot项目的配置注解,这也是一个组合注解:

   

  在Spring Boot项目中推荐使用@ SpringBootConfiguration替代@Configuration(@Configuration用于定义配置类,可替换xml配置文件)

  b.@EnableAutoConfiguration:启用自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项:

    a) 如:我们添加了spring-boot-starter-web的依赖,项目中也就会引入SpringMVC的依赖,Spring Boot就会自动配置tomcatSpringMVC

    

  c.@ComponentScan:默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。

3.看一下启动日志

当打印:Started HelloApplication in 3.866 seconds (JVM running for 5.621) 表示启动成功。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

上面的配置繁琐吗?  如果还觉得麻烦的话,再此推荐一个网站:http://start.spring.io/

构建结束之后,直接下载。然后在IDE中导致即可。

猜你喜欢

转载自www.cnblogs.com/muxi0407/p/8934010.html
今日推荐