spring boot笔记

1、Spring boot介绍

Spring Boot是pivotal提供的全新的框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。

2、Spring boot项目搭建

(1)在eclipse中搭建maven app项目,在pom.xml中引入相应的jar包

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>zlc</groupId>
    <artifactId>spring_boot</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_boot Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>spring_boot</finalName>
    </build>
</project>

(2)创建spring boot的入口类

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//开启包扫描,扫描controller,service等包下的spring组件
@SpringBootApplication(scanBasePackages="controller")
//开启spring boot默认配置
@EnableAutoConfiguration
public class StartApplication {
    //配置spring boot的启动类
    public static void main(String[] args) {
        //spring boot 内置了Tomcat,所以,springboot项目不用在外部Tomcat中启动
        //第一个参数应为当前类.class
        SpringApplication.run(StartApplication.class, args);
    }

}

(3)启动spring boot项目,就只需右击启动入口类StartApplication直接运行即可

运行效果如下:

 我们写一个Controller测试一下

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
    
   @ResponseBody
   @RequestMapping("/test")
   public String test() {
      return "I am a student";       
   }
   
}

运行结果:

3、对spring boot进行相关配置

在以下文件夹中新建一个application.properties文件,并指定Tomcat端口号

如图:

 

posted @ 2018-09-01 23:34  梦里下起了雪  阅读(126)  评论(0编辑  收藏  举报