IDEA SpringBoot的搭建及简单使用

Spring Boot是一个简化Spring开发的框架。用来监护spring应用开发,约定大于配置,去繁就简,just run 就能创建一个独立的,产品级的应用。

用来代替springMvc

Spring Boot 自带Tomcat

简单使用

1.IDEA创建springboot项目

  1. 选择Spring Initializr
  2. 设置启动项的包名和类名
  3. 选择Web–>Spring Web
  4. 点击finish完成
  5. 等待文件加载完成后
  • SpringbootApplication: 一个带有 main() 方法的类,用于启动应用程序
  • SpringbootApplicationTests:一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文
  • application.properties:一个空的 properties 文件,可以根据需要添加配置属性
  • pom.xml: Maven 构建说明文件
  1. 在java文件夹下新建com.action文件夹,另写一个测试类HelloAction
package com.action;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloAction {
    @RequestMapping("/hello")
    public String hello(){
        return "HelloWorld";
    }
}
  • @RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版

注意如果不在同一个包内要为启动函数添加扫描包名
如果有两个不同的包则要添加两个扫描且包名不同中的类名也不能相同
@ComponentScan({"包名1","包名2"})
为启动函数添加扫描

package cn.wmyskxz.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"com.action"})	//添加扫描
public class SpringbootApplication {

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

}

若有多个文件夹则添加多个扫描
@ComponentScan({“com.action”,“com.action2”})

  1. 运行启动函数

可以看到SpringBoot自带Tomcat端口号,默认为8080

  1. 打开浏览器输入http://127.0.0.1:8080/hello显示出HelloWorld即代表启动成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值