zoukankan      html  css  js  c++  java
  • 初识springboot

    简介

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

    功能

    • 创建独立的Spring applications
    • 能够使用内嵌的Tomcat, Jetty or Undertow,不需要部署war
    • 提供starter pom来简化maven配置
    • 自动配置Spring
    • 提供一些生产环境的特性,比如metrics, health checks and externalized configuration
    • 绝对没有代码生成和XML配置要求

    开篇

    • 如果你用过Spring JavaConfig的话,会发现虽然没有了xml配置的繁琐,但是使用各种注解导入也是很大的坑,
    • 然后在使用一下Spring Boot,你会有一缕清风拂过的感觉

    核心注解类说明

    @RestController

    就是@Controller+@ResponseBody组合,支持RESTful访问方式,返回结果都是json字符串

    @SpringBootApplication

    就是@SpringBootConfiguration+@EnableAutoConfiguration+ 
    @ComponentScan等组合在一下,非常简单,使用也方便

    @SpringBootTest

    Spring Boot版本1.4才出现的,具有Spring Boot支持的引导程序(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分) 

    配置文件pom.xml

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3     <modelVersion>4.0.0</modelVersion>
     4 
     5     <groupId>com.jege.spring.boot</groupId>
     6     <artifactId>spring-boot-hello-world</artifactId>
     7     <version>0.0.1-SNAPSHOT</version>
     8     <packaging>jar</packaging>
     9 
    10     <name>spring-boot-hello-world</name>
    11     <url>http://maven.apache.org</url>
    12 
    13     <!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <!-- 自动包含以下信息: -->
    17         <!-- 1.使用Java6编译级别 -->
    18         <!-- 2.使UTF-8编码 -->
    19         <!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). -->
    20         <!-- 4.智能资源过滤 -->
    21         <!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). -->
    22         <artifactId>spring-boot-starter-parent</artifactId>
    23         <!-- spring boot 1.x最后稳定版本 -->
    24         <version>1.4.1.RELEASE</version>
    25         <!-- 表示父模块pom的相对路径,这里没有值 -->
    26         <relativePath />
    27     </parent>
    28 
    29     <properties>
    30         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    31         <java.version>1.8</java.version>
    32     </properties>
    33 
    34     <dependencies>
    35 
    36         <!-- web -->
    37         <dependency>
    38             <groupId>org.springframework.boot</groupId>
    39             <artifactId>spring-boot-starter-web</artifactId>
    40         </dependency>
    41 
    42         <!-- 测试 -->
    43         <dependency>
    44             <groupId>org.springframework.boot</groupId>
    45             <artifactId>spring-boot-starter-test</artifactId>
    46             <!-- 只在test测试里面运行 -->
    47             <scope>test</scope>
    48         </dependency>
    49 
    50     </dependencies>
    51 
    52     <build>
    53         <finalName>spring-boot-hello-world</finalName>
    54         <plugins>
    55             <!-- jdk编译插件 -->
    56             <plugin>
    57                 <groupId>org.apache.maven.plugins</groupId>
    58                 <artifactId>maven-compiler-plugin</artifactId>
    59                 <configuration>
    60                     <source>${java.version}</source>
    61                     <target>${java.version}</target>
    62                 </configuration>
    63             </plugin>
    64         </plugins>
    65     </build>
    66 </project>
    View Code

    启动类Application

     1 import org.springframework.boot.SpringApplication;
     2 import org.springframework.boot.autoconfigure.SpringBootApplication;
     3 
     4 /**
     5  * @author JE哥
     6  * @email 1272434821@qq.com
     7  * @description:spring boot 启动类
     8  */
     9 
    10 @SpringBootApplication
    11 public class Application {
    12 
    13   public static void main(String[] args) {
    14     SpringApplication.run(Application.class, args);
    15   }
    16 
    17 }
    View Code

    控制器HelloWorldController

     1 import java.util.Arrays;
     2 import java.util.List;
     3 
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RestController;
     6 
     7 /**
     8  * @author JE哥
     9  * @email 1272434821@qq.com
    10  * @description:看看spring-boot的强大和方便
    11  */
    12 @RestController
    13 public class HelloWorldController {
    14 
    15   @RequestMapping("/hello1")
    16   public String hello1() {
    17     return "Hello World";
    18   }
    19 
    20   @RequestMapping("/hello2")
    21   public List<String> hello2() {
    22     return Arrays.asList(new String[] { "A", "B", "C" });
    23   }
    24 }
    View Code
  • 相关阅读:
    CopyOnWriteArrayList 读写分离,弱一致性
    Java中定时器Timer致命缺点(附学习方法)
    排队打饭:公平锁和非公平锁(面试)
    母鸡下蛋实例:多线程通信生产者和消费者wait/notify和condition/await/signal条件队列
    volatile,synchronized可见性,有序性,原子性代码证明(基础硬核)
    Synchronized用法原理和锁优化升级过程(面试)
    Java中多线程安全问题实例分析
    iOS 相互引用引起内存泄露问题说明
    iOS app 集成友盟推送问题
    ios即时通讯客户端开发之-mac上基于XMPP的聊天客户端开发环境搭建
  • 原文地址:https://www.cnblogs.com/yy136/p/8505074.html
Copyright © 2011-2022 走看看