SpringBoot2笔记2-依赖管理特性、自动配置特性

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所有支持的场景 见到的
  4. *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
所有场景启动器最底层的依赖
<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>

2.自动配置特性

自动配好Tomcat
  1. 引入Tomcat依赖。
  2. 配置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开发的常见场景

public static void main(String[] args) {
    //返回我们IOC容器
    ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

    //查看容器里面所有组件的函数
    String[] names = run.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }
}
默认的包结构

主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
无需以前的包扫描配置
想要改变扫描路径:
@SpringBootApplication(scanBasePackages=“com.lun”)
@ComponentScan 指定扫描路径

@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.lun")
各种配置拥有默认值
  1. 默认配置最终都是映射到某个类上,如:MultipartProperties
  2. 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
按需加载所有自动配置项
  1. 非常多的starter
  2. 引入了哪些场景这个场景的自动配置才会开启
  3. SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面
在config包中新建一个MyConfig.class

在这里插入图片描述
MyConfig.class

@Configuration(proxyBeanMethods = true) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {

    /**
     * Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
     * @return
     */
    @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
    public User user01(){
        User joy = new User("joy187", 18);
        //user组件依赖了Pet组件
        joy.setPet(tomcatPet());
        return joy;
    }

    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }

}

为了进行测试,要创建一个User.class

package com.atgg.boot.bean;

public class User {
    private String name;
    private Integer age;

    private Pet pet;

    public Pet getPet(){
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public User() { }
    public User (String name,Integer age) {
        this.name = name;
        this.age = age;
    }
    public String getName () { return name; }
    public void setName(String name) { this.name = name; }
    public Integer getAge( ) { return age; }
    public void setAge( Integer age) { this.age = age; }

    @Override
    public String toString( ) {
        return "User{" +
                "name='" + name + '\''+
                ", age='" + age + '\''+
                ", pet='" + pet + '\''+
                '}';
    }


}

创建一个Pet类:

package com.atgg.boot.bean;

public class Pet {
    private String name;

    public Pet() {}
    public Pet(String name) {
        this.name=name;
    }
    public String getName() { return name; }
    public void setName (String name) { this.name = name; }

    @Override
    public String toString( ) {
        return "Pet{" + "name='" + name + '\''+ '}';
    }
}
自动生成

对着需要生成getter,setter的变量按下“Alt”+“Enter”键,就可以自动生成方法。
在这里插入图片描述

也可以对着变量右键然后点击Generate…进行自动生成:

在这里插入图片描述

当proxyBeanMethods = true时,在MainApplication.java中进行测试:
package com.atgg.boot;

import com.atgg.boot.bean.Pet;
import com.atgg.boot.bean.User;
import com.atgg.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

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

        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        MyConfig 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));
    }

}

此时显示的为一个单例实体,返回true:
在这里插入图片描述
最佳实战

  1. 配置 类组件之间无依赖关系用Lite模式(proxyBeanMethods = false)加速容器启动过程,减少判断
  2. 配置 类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式(默认)(proxyBeanMethods = true)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值