标签:入门 Spring boot springframework Boot spring org
更多信息:
本篇算是学习笔记
视频:https://www.bilibili.com/video/BV1Et411Y7tQ?p=7
尚硅谷:http://www.atguigu.com/
编辑器:IntelliJ IDEA 2020.1
Spring Boot 的概念
一个框架;
J2EE一站式解决方案;
Spring Boot 是整个Spring技术栈的整合;
Spring Boot 的意义
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
简化Spring应用的开发
Spring Boot 的背景
J2EE笨重的开发、繁多的配置、低下的开发效率、
复杂的部署流程、第三方技术集成难度大。
Spring Boot 的优点与缺点
优点:
- 快速创建独立运行的Spring项目以及与主流框架集成
- 使用嵌入式的Servlet容器,应用无需打成WAR包;
- starters(启动器)自动依赖与版本控制;
- 大量的自动配置,简化开发,也可修改默认值;
- 无需配置XML,无代码生成,开箱即用;
- 准生产环境的运行时应用监控;
- 与云计算的天然集成;
缺点:
入门容易,难以精通;
原因:
Spring Boot是Spring上的封装,如果你想对Spring的内部不是很了解,那么你就很难对Spring Boot精通。
正是Spring Boot的对Spring的封装,让它可以快速独立的运行,但也正是它的封装让Spring中API接口对我们透明。
微服务
Martin Fowler对微服务一篇介绍微服务的博客让微服务公之于众。
博客链接:https://martinfowler.com/articles/microservices.html#MicroservicesAndSoa
微服务的概念
一种架构风格;
一个应用一个是一组小型服务,每一个小型服务都可以独立替换和升级的小型软件单元;
(单体应用: ALL IN ONE;)
微服务之间部署和运维
Spring Boot 的Hello world程序
步骤一
创建一个Maven工程
步骤二
导入Spring Boot 的依赖,将下面的代码放到Maven项目的Pom.xml中
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
步骤三
创建一个主程序,启动Spring Boot 的应用
步骤四
编写一个主程序
package com.atguigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication: 标注主程序为一个Spring Boot 应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Question:SpringBootApplication.run(HelloWorldMainApplication.class,args);
//启动Spring Boot 应用
SpringApplication.run(HelloWorldMainApplication.class, args);
//详解https://blog.csdn.net/weixin_41884010/article/details/88844946
}
}
步骤五
编写Controller、Service
package com.atguigu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @ClassName: HelloController
* @Description:
* @Author: 18541900
* @Date 2020/12/16
*/
//处理请求
@Controller
public class HelloController {
@ResponseBody //把 Hello World! 写到浏览器中去
@RequestMapping("/hello")//接受来自浏览器的 /hello 请求
public String hello() {
return "Hello World!";//接受后的动作, Hello World! 返回给浏览器
}
}
然后启动HelloWorldMainApplication
启动之后,没有报错的话,打开浏览器输入网址 http://localhost:8080/hello。
步骤六
将下面的代码放到Maven项目的Pom.xml中,简化部署,将程序打包成为一个可执行的Jar包。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包方法如下
运行结果
我们到jar包所在目录 ,或是将jar包复制到别的位置运行
运行并成功后,我们也可以在浏览器上访问到下图
Spring Boot 的Hello world程序的原理
文件结构
Pom文件
父项目
<!--图与代码顺序是对应的-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!--它的父项目是-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<!--作用:真正管理Spring Boot 应用里面所有依赖版本(Spring版本仲裁中心)-->
<!--以后我们导入依赖默认是不需要写版本;
(没有在dependencies里面管理的依赖自然需要声明版本号)-->
启动器
帮我们导入了web模块正常运行所依赖的组件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!--spring-boot-starter:spring-boot场景启动器
web: web模块
总结:Spring Boot将所有的功能场景都抽取出来,
做成一个个的starters(启动器),
只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。
要用什么功能就导入什么场景的启动器-->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
自动配置的原理
@SpringBootApplication
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//Spring Boot配置类:表示这个类是Spring Boot的配置类
@SpringBootConfiguration
//开启自动配置功能
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
@SpringBootConfiguration
@EnableAutoConfiguration
自动配置功能,Spring Boot来帮我们自动配置,这个注解告诉Spring Boot开启自动配置。
EnableAutoConfigurationImportSelector的父类AutoConfigurationImportSelector
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;
Spring Boot 的快速创建
前提:要有网络,Spring Boot才能帮你配置
步骤一
步骤二
步骤三
步骤四
文件结构
Controller
package com.atguigu.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*@ResponseBody //作用: 这个类的所用方法返回的数据直接写给浏览器
@Controller*/
@RestController //作用: 对注解 @ResponseBody 和 @Controller 的整合
//@RequestMapping//("/login")
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world quick!";
}
}
特点
默认生成的Spring Boot项目;
- 主程序已经生成好了,我们只需要写我们自己的逻辑;
- resources文件夹中目录结构;
- static:保存所有的静态资源; js css images;
- templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;
标签:入门,Spring,boot,springframework,Boot,spring,org 来源: https://blog.csdn.net/sherlocklcy/article/details/111239028
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。