一、Spring Boot:入门

一、Spring Boot概述

1. 什么是Spring Boot?

2. Spring Boot优点

  1. 敏捷开发
  2. 减少xml配置文件,用properties文件
  3. 内置web容器

二、入门案例(Idea)

1. 创建Spring Initializr

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 添加spring boot父类依赖和spring boot web依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <!--引入springboot父类依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lt</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <!--spring boot-web依赖 spring mvc+spring+mybatis-->
    <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>
    </build>

</project>

3. 创建测试类,并创建主函数

package com.lt.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName HelloWorld
 * @Description spring boot 例子
 * @Author lt
 * @Date 2019-04-05
 **/

/**
 * 1.@RestController:表示该类中所有方法的返回值都是json格式
 * 2.@EnableAutoConfiguration
 */
@RestController
public class HelloWorld {
    @RequestMapping("/getMsg")
    public String getMsg() {
        return "你好,Spring Boot";
    }
    public static void main(String[] args){
        SpringApplication.run(HelloWorld.class, args);
    }
}

4. 启动项目,访问

在这里插入图片描述

三、Spring Boot常用注解

  1. @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解;
  2. @ComponentScan:让spring boot扫描到Configuration类并把它加入到程序上下文
  3. @Configuration:等同于spring的XML配置文件;使用Java代码可以检查类型安全
  4. @EnableAutoConfiguration:自动配置
  5. @RestController:@Controller和@ResponseBody的合集,表示该类中所有方法的返回值都是json格式

四、spring boot两种启动方式

1. 单独启动:直接运行main方法

package com.lt.springboot.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Login
 * @Description TODO
 * @Author lt
 * @Date 2019-04-05
 **/
@RestController
public class Login {

    @RequestMapping("/login")
    public Map<String, Object> login(){
        Map<String, Object> map = new HashMap<>();
        map.put("username", "刘涛");
        map.put("password", "123456");
        return map;
    }
    public static void main(String[] args){
        SpringApplication.run(Login.class, args);
    }
}

2. 设置包扫描路径

  • 利用注解@ComponentScan(“com.lt.springboot”)
package com.lt.springboot.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * @ClassName App
 * @Description TODO
 * @Author lt
 * @Date 2019-04-05
 **/
@ComponentScan("com.lt.springboot")
@EnableAutoConfiguration
public class App {

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

五、静态资源访问

1. spring boot默认配置

  1. Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
  • /static
  • /public
  • /resources
  • /META-INF/resources
    在这里插入图片描述
  1. 访问
    在这里插入图片描述

六、全局捕获异常

其他

ideal配置git

IDEA配置github并上传项目

参考

【1】纯洁的微笑
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值