SpringBoot
为什么使用SpringBoot
-
SpringBoot能快速创建出生产级别的Spring应用
SpringBoot的优点
-
创建独立Spring应用 -
内嵌web服务器,不用再去配置服务器 -
自动starter依赖,简化构建配置,引入对应的starter,SpringBoot会自动导入相关的依赖 -
自动配置Spring以及第三方功能 -
提供生产级别的监控、健康检查及外部化配置 -
无代码生成、无需编写XML,不需要配置复杂的配置文件
“
综上所述: SpringBoot是整合Spring技术栈的一站式框架 SpringBoot是简化Spring技术栈的快速开发脚手架
”
缺点
-
迭代快,需要时刻关注版本的迭代更新 -
封装太深,内部原理复杂.不容易精通
编写第一个SpringBoot程序
版本要求
-
JDK 8 及以上 -
Maven 3.3以上
导入SpringBoot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建主程序类
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
-
@SpringBootApplication注解的作用是:表明这是一个SpringBoot配置类(依赖自动导入等)
编写测试程序
-
在MainApplication同级的目录下新建controller包,包中新建Controller类如下
@RestController // 相当于 @Controller @ResponseBody的合体
public class HelloController {
@RequestMapping("/hello")
public String handle01() {
return "Hello,SpringBoot 2";
}
}
项目的整体结构
测试运行
SpringBoot的配置文件
SpringBoot的快速打包,简化部署
-
不需要在pom文件中配置打包形式war包等,直接在pom文件中导入maven插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-
执行mvn package命令 -
自动打包成jar包,可在服务器直接执行,命令行使用 java -jar springboot-study-1.0-SNAPSHOT.jar
-
如果windows窗口启动项目跑一半暂停,可能是因为鼠标点击了窗口,导致项目启动暂停,需要关闭cmd的快速编辑模式,右击cmd窗口,选择默认值,关闭快速编辑模式
SpringBoot的依赖管理
-
在SpringBoot的父项目中,几乎声明到了所有开发中常用的依赖以及版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
-
在SpringBoot中需要开发什么功能只需要导入相关的starter场景启动器,比如web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-
无需关注导入的版本号,springBoot会自动仲裁,如果需要引入非仲裁版本中的仲裁jar,则配置版本号
<properties>
<mysql.version>5.1.43</mysql.version>
</properties>
SpringBoot的组件添加(使用配置文件)
-
首先新建配置文件,声明@Configuration,声明这是一个配置类,在类中书写带返回值的方法,使用@Bean注解进行标注,代表 给容器中添加组件,以方法名作为组件的id,返回类型就是组件类型,返回的值,就是组件在容器中的实例,默认是单实例,在bean中添加名字,则代表注入的实例名字
@Configuration // 告诉SpringBoot这是一个配置类,等同于配置文件
public class MyConfig {
@Bean // 给容器中添加组件,以方法名作为组件的id,返回类型就是组件类型,返回的值,就是组件在容器中的实例
public User user01(){
return new User("zhangsan",12);
}
@Bean("tom")
public Pet tomcatPet(){
return new Pet("tomcatCat");
}
}
-
测试上述的组件是否添加
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
// 打印出所有注入的组件的名字
String[] beanDefinitionNames = run.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
// 从容器中获取组件,默认为单例模式,获取的都是一个实例
Pet tom01 = run.getBean("tom",Pet.class);
// 如果@Configuration(proxyBeanMethods = true) 代理对象调用方法,SprngBoot总会检查这个组件是否在容器中有
// 保证组件在容器中单实例
Pet tom02 = run.getBean("tom",Pet.class);
System.out.println(tom01==tom02);
}
}
“
配置类上的 @Configuration(proxyBeanMethods = true) 注解,当proxyBeanMethods 为true时,代表使用代理对象方法,表示该配置类中的所有相关注入方法只生成一个实例对象,所以上述才会输出true,当需要生成多个对象时,可以将该值置为false,则每一次生成都是一个新的对象 Full(proxyBeanMethods = true) 全模式 单例,容器中对象唯一,启动时检查容器中是否有该组件实例 Lite(proxyBeanMethods = false) 轻量级,不再是单例 ,不会检查,直接new
”
导入类的第二种方法
-
在配置类上增加@Import注解,给容器中自动创建出组件、默认组件的名字就是全类名
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = true) // 告诉SpringBoot这是一个配置类,等同于配置文件
public class MyConfig {
}
导入外部文件进行添加组件
-
配置类上使用@ImportResource(“classpath:beans.xml”) 导入外部的Spring配置文件(Spring的原生配置文件)
条件添加注解@Conditional
@Configuration(proxyBeanMethods = true) // 告诉SpringBoot这是一个配置类,等同于配置文件
public class MyConfig {
//@ConditionalOnBean(name="tom") 判断当容器中有tom组件时,注册user01组件,可以标注在类上,也可以标注在方法上
//@ConditionalOnMissingBean(name="tom") 当容器中没有tom时装配
@ConditionalOnBean(name="tom") //当容器中有tom时进行装配
@Bean // 给容器中添加组件,以方法名作为组件的id,返回类型就是组件类型,返回的值,就是组件在容器中的实例
public User user01(){
return new User("zhangsan",12);
}
@Bean("tom")
public Pet tomcatPet(){
return new Pet("tomcatCat");
}
}
SpringBoot配置文件与类的绑定
-
新定义Car类型
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + ''' +
", price=" + price +
'}';
}
}
-
配置文件配置属性
mycar.brand = BYD
mycar.price = 1000000
-
将配置文件与类相绑定一共有两种方式,无论哪种方式,都是先将实体类注入到容器中,只有容器中的对象,才能使用SpringBoot的功能
-
在上述的Car类上添加@Component + @ConfigurationProperties ,@Component 注解代表将该类注入到容器,@ConfigurationProperties 代表将配置文件中对应前缀的属性与类相绑定 @Component
@ConfigurationProperties(prefix = "mycar") -
使用@EnableConfigurationProperties + @ConfigurationProperties,@ConfigurationProperties还是在对应的实体类上,但是@EnableConfigurationProperties 需要在配置类上配置,这个注解会将实体类导入到容器中 @Configuration(proxyBeanMethods = true) // 告诉SpringBoot这是一个配置类,等同于配置文件
@EnableConfigurationProperties(Car.class)
public class MyConfig {
}
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + ''' +
", price=" + price +
'}';
}
}
-
测试,在Controller中写测试类,返回该Car对象,查看输出内容
@RestController
public class HelloController {
@Autowired
Car car;
@RequestMapping("/car")
public Car getCar() {
return car;
}
}
“
尚硅谷B站SpringBoot教程: SpringBoot2零基础入门
”
原文始发于微信公众号(卫颓废):SpringBoot-基础入门
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/47541.html