1、SpringBoot特点
1.1、依赖管理
- 父项目做依赖管理
- 依赖管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
他的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制
- 开发导入starter场景启动器
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、所有场景启动器最底层的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
- 无需关注版本号,自动版本仲裁
1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar,要写版本号。 - 可以修改默认版本号
- 1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置
<properties>
<mysql.version>5.1.43</mysql.version>
</properties>
1.2、自动配置
• 自动配好Tomcat
• 引入Tomcat依赖。
• 配置Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
• 自动配好SpringMVC
• 引入SpringMVC全套组件
• 自动配好SpringMVC常用组件(功能)
• 自动配好Web常见功能,如:字符编码问题
• SpringBoot帮我们配置好了所有web开发的常见场景
• 默认的包结构
• 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
• 无需以前的包扫描配置
• 想要改变扫描路径,@SpringBootApplication(scanBasePackages=“com.atguigu”)
• 或者**@ComponentScan** 指定扫描路径
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(“com.atguigu.boot”)
• 各种配置拥有默认值
• 默认配置最终都是映射到某个类上,如:MultipartProperties
• 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
• 按需加载所有自动配置项
• 非常多的starter
• 引入了哪些场景这个场景的自动配置才会开启
• SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面
•
• …
2、容器功能
主要是几个注解@Configuration、、@Bean、@Component、@Controller、@Service、@Repository@ComponentScan、@Import@Conditional@ImportResource@ConfigurationProperties@EnableConfigurationProperties + @ConfigurationProperties@Component + @ConfigurationProperties
几个注解
@SpringBootApplication
作用在主程序上
等同于下面的三个注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(“ustc.gzy.boot”)
@Configuration(proxyBeanMethods = false)
-
用在配置类Myconfig(用来配置容器,如注册bean等)的上面
-
告诉SpringBoot这是一个配置类 == 配置文件
-
proxyBeanMethods代理bean的方法,=true表示单例模式,=false为多例模式。
-
Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
-
Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
-
组件依赖必须使用Full模式默认。其他默认是否Lite模式
@Bean
- 使用在配置类上,和@Configuration一块,用来注册bean
- 以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
- @Bean(“tom22”)//自定义名字tom22
@Component、@Controller、@Service、@Repository
@component是spring中的一个注解,它的作用就是实现bean的注入,@component取代。在探究@component前先了解一下注解?何为注解?注解本质上就是一个类,开发中我们可以使用注解 取代 xml配置文件。
web开发,提供3个@Component注解衍生注解(功能一样)取代
@Repository(“名称”):dao层
@Service(“名称”):service层
@Controller(“名称”):web层
@Autowired:自动根据类型注入
@Qualifier(“名称”):指定自动注入的id名称
@Import
@Import({User.class, DBHelper.class})
- 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
@Conditional
@ConditionOnBean:tom这个bean存在的时候才将这个被标注的组件加入容器。和@Bean一快用,可以用在类上
@ConditionalOnBean(name = "tom22")//当容器中存在tom22时才进行user01组件添加
@Bean//给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
User gzy = new User("gzy", 18);
return gzy;
}
@ConditionalOnMissingBean(" ");条件是不存在指定的bean时执行下面的配置。
@ImportResource原生配置文件引入
- 作用:导入xml配置文件,使用xml来注册组件
======================beans.xml=========================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
<bean id="hehe" class="com.atguigu.boot.bean.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>
@ImportResource("classpath:beans.xml")
public class MyConfig {}
======================测试=================
boolean haha = run.containsBean("haha");
boolean hehe = run.containsBean("hehe");
System.out.println("haha:"+haha);//true
System.out.println("hehe:"+hehe);//true
@ConfigurationProperties配置绑定
- 读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用
@ConfigurationProperties
注意:写配置文件时需要加上前缀名。
application.properties中的内容:
mycar.brand=BYD
mycar.price=100000
/**
* 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
*/
@Component
@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 +
'}';
}
}
@Component//不要忘记
@ConfigurationProperties
也可以@EnableConfigurationProperties + @ConfigurationProperties代替
其中@EnableConfigurationProperties是放在配置类上
@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中