从零开始的Spring Boot (一)

从零开始的Spring Boot (一)

官方网址:http://projects.spring.io/spring-boot/

What is Spring boot?

关于Spring Boot的说明直接看官网的比较准确,因此不多赘述,Spring Boot的出现我认为属于是SpringMVC的配置简化版,也就是约定大于配置(个人这么认为)免去了SpringMVC开发时候的一大堆的配置,官方说法 “just run”!。不多说,直接开始。

HelloWorld!

环境:eclipse + maven + jdk1.8

  1. 创建一个Maven项目
    这里写图片描述

    这里直接选择Create a simple project,下一步,finish就行了。

  2. pom.xml导入Spring Boot依赖
<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</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
     <!-- Spring Boot web模块 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

  <build>
    <plugins>
        <!-- 为了使用jdk1.8加入此项配置 maven默认使用1.5版本 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>
  1. 创建HelloWorld.Java
package helloworld;

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;

@EnableAutoConfiguration
@Controller
public class HelloWorld {

    @RequestMapping("/hello")
    public @ResponseBody String sayHello(){
        return "hello world";
    }

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

这里的@EnableAutoConfiguration很明显是让SpringBoot自动配置的意思。SpringBoot会根据pom导入的包尽可能的判断我们想要干什么从从而自动的配置。@Controller声明这是一个Controller,最后加上@ResponseBody注解返回json数据方便页面上显示。

  1. 运行HelloWorld.java(run as java application), 启动完成后打开浏览器输入localhost:8080/hello/
    这里写图片描述
    至此一个简单的helloworld就完成了。

Question?

  1. spring-boot-starter-web都有些什么包?
  2. 服务器?
    这里写图片描述
    这里写图片描述
    可以看到其中包含了springboot的包,springMVC中常见的webmvc,aop,jackson等熟悉的包,tomcat包以及日志包。说白了spring-boot-starter-web也就是个集中营压缩包的感觉。
    在看看官方说明:
    这里写图片描述
    • 可以看出springboot内嵌了tomcat,jetty,Undertow(JBoss下的一款服务器)。
      并且springboot默认使用的是tomcat作为服务器,如果想要切换jetty或者Undertow的话只需要修改pom文件即可(自动配置)。
      很简单,先排除spring-boot-starter-web中的tomcat组件再导入jetty或者Undertow的包即可.(以jetty为例)
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    </dependencies>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值