springboot的基本概述 ,编写第一个springboot工程

Lxw


前言

在普通的java项目中,大量的XML文件配置起来是很繁琐就会导致开发效率低,整合第三方框架的配置可能存在冲突问题导致部署效率低,还有其它的问题,传统java项目的打包方式:打包成一个war放入到tomcatwebapps目录下进行执行,也就是说需要依赖外部的tomcat服务器才能执行。
而SpringBoot可以省去很多麻烦的配置文件等,还可以快速创建独立运行的Spring项目以及与主流框架集成......
让我们看看springboot的快速搭建以及他的优点吧!👇

一、SpringBoot是什么?

Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。

二、SpringBoot优点?

快速创建独立运行的Spring项目以及与主流框架集成
使用嵌入式的Servlet容器,应用无需打成WAR包
starters自动依赖与版本控制
大量的自动配置,简化开发,也可修改默认值
无需配置XML,无代码生成,开箱即用
准生产环境的运行时应用监控
与云计算的天然集成

三、编写第一个springboot工程

1.1.2.1 创建maven工程
步骤:
使用idea工具创建一个maven工程,该工程为普通的java工程即可
在这里插入图片描述

点击Next
在这里插入图片描述
在这里插入图片描述

点击Next

点击Finish
1.1.2.2 添加依赖
在pom.xml里面导入起步依赖

org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE

SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖

org.springframework.boot spring-boot-starter-web

1.1.2.3 编写controller
在这里插入图片描述

1.2.2.4编写启动类
在这里插入图片描述
@EnableAutoConfiguration注解:作用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置
这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。
@EnableAutoConfiguration扫描的时候,只能扫描到当前类

@ComponentScan注解
由于@EnableAutoConfiguration注解只能扫描当前的类,这样对Controller里面的类进行管理很不方便,这个时候,我们可以用@ComponentScan注解来配置扫描包的范围。
我们可以将启动器抽取成一个单独的类
@EnableAutoConfiguration
@ComponentScan(“com.oracle.controller”)
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}

但是使用@ComponentScan进行扫包的时候,包比较多的情况下,写起来比较麻烦。比如我要扫码多个包:
@ComponentScan(basePackages = {“com.oracle.user.controller”,“com.oracle.order.controller”})

我们可以使用@SpringBootApplication,一劳永逸的解决以上问题
@SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰,换言之 Springboot 提供了统一的注解来替代以上三个注解
扫包范围:在启动类上加上@SpringBootApplication注解,当前包下或者子包下所有的类都可以扫到。
1.2.2.5 使用@SpringBootApplication注解编写启动类

但是此时需要注意App类所在的包中的位置!

**

最终代码

**

pom文件(示例):
可能会报错,因为没有这个依赖,需要下载,如果下载特别慢或者下载不下来,可能就是你的maven中的setting文件有问题,去修改下一仓库地址 ,设置为阿里的就好了 ,下载点击maven右边那个进行下载。不报错之后就可以下一步操作了!在这里插入图片描述

<?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>

    <groupId>cn.lxw</groupId>
    <artifactId>spring_boot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--spring-boot-starter-parent整合第三方常用框架的依赖信息-->
    <!--在pom文件里面引入父级依赖(springboot项目在启动的时候,需要进行自动的配置)-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <!--在pom文件里面引入web启动器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Controller层(示例):

package cn.jiyun.controller;

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

@RestController
@RequestMapping("hello")
public class HelloController {
    @RequestMapping("test")
    public String sayHello() {
        return "hello,SpringBoot,   My name is  lxw";
    }

}

App类(示例):

package cn.jiyun.controller;

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

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

执行main方法 访问路径 localhost:8080/hello/test 就可以看到返回的字符串了

在这里插入图片描述

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了springboot的基本概述 ,编写第一个springboot工程。

1.1 springboot的基本概述
1.2 编写第一个springboot工程
1.2.1 使用maven工具构建一个maven项目
1.2.2 在pom文件里面引入父级依赖(springboot项目在启动的时候,需要进行自动的配置)

org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE

1.2.3 在pom文件里面引入web启动器

org.springframework.boot
spring-boot-starter-web

1.2.4 编写controller
1.2.5 编写一个springboot的启动类。来启动我们的springboot项目
@SpringbootApplication
public class App{
public static void main(String[] args){
SpringApplication.run(App.class,args);
}
}
1.3 构建springboot项目需要注意的细节
1.3.1 parent标签里面到底定义了什么?
spring-boot-starter-parent继承了spring-boot-dependencies,在spring-boot-dependencies
的里面定义了大量的第三方技术的依赖,并做了相应的版本控制。我们只需要引入某个技术的启动器,如果
这个启动器在spring-boot-starter-parent里面存在,那么springboot项目在运行的时候,会进行对应启动器
的技术的自动配置
1.3.2 spring-boot-starter-web里面定义了什么?
json格式数据转换的依赖、springmvc的依赖、tomcat的依赖…
1.3.3 springboot如何进行包扫描?
在springboot的启动器上,有一个@SpringbootApplication注解,在这个注解下面,有一个@ComponentScan
注解,这个注解专门进行包扫描的操作,扫描的规则是:扫描当前注解所在的类同级的包以及下面的所有子包。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值