此处以我自己使用的版本为例,在后期学习过程中遇到一些问题,我也会试着给出解决方案
配置我们Maven的 setting.xml
指定了镜像源和使用的JDK版本
nexus-aliyun central Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public jdk-1.8 true 1.8 1.8 1.8 1.8
需求:通过浏览器发送 /hello请求,响应 Hello, Spring Boot 2! 到浏览器中
🌔 附上目录结构:
(1)步骤一:在IDEA 中创建好我们的工程,在pom.xml 添加依赖
org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE org.springframework.boot spring-boot-starter-web
(2)步骤二:需要创建我们的主程序 【用来引导SpringBoot项目的启动】
package com.atguigu.boot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author Bonbons* @version 1.0* 这个SpringBootApplication注解的作用就是告诉SpringBoot,这是一个SpringBoot的应用* 被标记的这个类我们称为主程序类*/
@SpringBootApplication
public class MainApplication {public static void main(String[] args) {//运行主程序,将主程序类加载到内存中,并传递主程序的参数SpringApplication.run(MainApplication.class, args);}
}
(3)步骤三:编写业务,也就是处理请求的控制器
package com.atguigu.boot.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;/*** @author Bonbons* @version 1.0*/
@SuppressWarnings("ALL") //抑制警告
//@Controller //声明Bean(控制器)
//@ResponseBody //说明我们这个类里返回的字符串都是用来响应浏览器的,而不是用来跳转
@RestController //属于Controller+ResponseBody
public class HelloController {@RequestMapping("/hello") //请求映射public String hello(){return "Hello, String Boot 2!"; //响应数据}
}
(4)步骤四:运行main方法进行测试
如图所示测试成功
简化配置,我们在resources文件夹下创建一个 application.properties
# 修改tomcat服务器端口号
server.port=8888
# SpringBoot相关的所有配置都防止在这个文件中,如果我们不主动更改,都会采取默认的配置
简化部署:在pom.xml 文件中添加一段配置,我们通过maven的 clean + package 打出来的 jar 包可以在DOS中直接运行
org.springframework.boot spring-boot-maven-plugin
依赖管理
org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE
他的父项目org.springframework.boot spring-boot-dependencies 2.3.4.RELEASE 几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制
1、见到很多 spring-boot-starter-* : *就某种场景
2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
3、SpringBoot所有支持的场景
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
4、见到的 *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
5、所有场景启动器最底层的依赖
org.springframework.boot spring-boot-starter 2.3.4.RELEASE compile
1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置5.1.43
org.springframework.boot spring-boot-starter-tomcat 2.3.4.RELEASE compile
自动配好 SpringMVC
自动配好Web常见功能,如:字符编码问题
默认的包结构:
主程序所在包及其下面的所有子包里面的组件会默认扫描出来【和MainApplication在一个包下】
无需以前的包扫描配置
想要改变扫描路径,
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
各种配置拥有默认值
按需加载所有自动配置项
那么在SpringBoot中,我们应该如何添加组件呢?
配置类可以编写全模式和轻量级模式,就是通过proxyBeanMethods属性的设置
配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
#############################Configuration使用示例######################################################
/*** 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的* 2、配置类本身也是组件* 3、proxyBeanMethods:代理bean的方法* Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】* Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】* 组件依赖必须使用Full模式默认。其他默认是否Lite模式****/
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {/*** Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象* @return*/@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例public User user01(){User zhangsan = new User("zhangsan", 18);//user组件依赖了Pet组件zhangsan.setPet(tomcatPet());return zhangsan;}@Bean("tom")public Pet tomcatPet(){return new Pet("tomcat");}
}################################@Configuration测试代码如下########################################
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
public class MainApplication {public static void main(String[] args) {//1、返回我们IOC容器ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);//2、查看容器里面的组件String[] names = run.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}//3、从容器中获取组件Pet tom01 = run.getBean("tom", Pet.class);Pet tom02 = run.getBean("tom", Pet.class);System.out.println("组件:"+(tom01 == tom02));//4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892MyConfig bean = run.getBean(MyConfig.class);System.out.println(bean);//如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。//保持组件单实例User user = bean.user01();User user1 = bean.user01();System.out.println(user == user1);User user01 = run.getBean("user01", User.class);Pet tom = run.getBean("tom", Pet.class);System.out.println("用户的宠物:"+(user01.getPet() == tom));}
}
组件放在默认包扫描的范围内,之前的那些注解也能用
@Import注解的作用:导入指定类型的组件,自动调用指定组件的无参构造器创建指定组件的对象获取到的组件名字就是默认的全类名
@Conditional:按照某些指定的条件进行装配 【条件装配】
=====================测试条件装配==========================
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
//@ConditionalOnBean(name = "tom")
@ConditionalOnMissingBean(name = "tom")
public class MyConfig {/*** Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象* @return*/@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例public User user01(){User zhangsan = new User("zhangsan", 18);//user组件依赖了Pet组件zhangsan.setPet(tomcatPet());return zhangsan;}@Bean("tom22")public Pet tomcatPet(){return new Pet("tomcat");}
}public static void main(String[] args) {//1、返回我们IOC容器ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);//2、查看容器里面的组件String[] names = run.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}boolean tom = run.containsBean("tom");System.out.println("容器中Tom组件:"+tom);boolean user01 = run.containsBean("user01");System.out.println("容器中user01组件:"+user01);boolean tom22 = run.containsBean("tom22");System.out.println("容器中tom22组件:"+tom22);}
🌔 原生配置文件的导入
🌔 配置绑定机制
配置绑定:
只有在容器中的组件,才能使用SpringBoot提供的强大功能
zbc.carNo=123;zbc.carName=benchi
,如果我们想对Car这个类使用配置绑定,就要将Car这个类上添加@Component注解,然后添加@ConfigurationProperties(prefix=“zbc”) //prefix 是前缀的意思方式1:@Component + @ConfigurationProperties
方式2:@EnableConfigurationProperties【开启属性绑定】【适用于我们要用第三方设置的类】
自动装配适用于容器中已经有的组件 @Autowire
🌔 1、我们需要知道 @SpringBootApplication 注解标记一个主程序启动类,它是以下三个注解的组合
🌔 2、banner图的两种配置方式:
spring.banner.image.location=图片路径
修改banner图🌔 3、总结
🌔 4、最佳实践
🌔 1、使用Lombok简化开发
org.projectlombok lombok
===============================简化JavaBean开发===================================
@NoArgsConstructor
//@AllArgsConstructor
@Data
@ToString
@EqualsAndHashCode
public class User {private String name;private Integer age;private Pet pet;public User(String name,Integer age){this.name = name;this.age = age;}}================================简化日志开发===================================
@Slf4j
@RestController
public class HelloController {@RequestMapping("/hello")public String handle01(@RequestParam("name") String name){log.info("请求进来了....");return "Hello, Spring Boot 2!"+"你好:"+name;}
}
🌔 2、dev-tools
org.springframework.boot spring-boot-devtools true
🌔 3、Spring Initailizr