SpringBoot2入门|依赖管理特性+自动配置特性+底层注解

SpringBoot特点——依赖管理与自动配置

依赖管理:配置文件pom.xml
  1. 按ctrl点击spring-boot-starter-parent可进入其.pom文件,可以查看父项目的配置,不断重复“ctrl+点击”动作一层一层向上找,直至找到根节点项目的配置,可以看到很多依赖的版本号
  2. 子项目继承父项目的所有依赖,而根节点的项目有几乎所有常用依赖的版本,所以子项目中的版本号可以不写
	<!--子项目-->
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> 
    </parent>
	<!--父项目-->
  	<parent>	
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-dependencies</artifactId>
	    <version>2.4.2</version>
  	</parent>

依赖管理

  1. 如果需要自定义适合的版本号,则去mvnrepository.com搜索相应版本号,在pom.xml文件中添加以下配置,点击左侧栏maven图示按钮,进行刷新,可以在右侧栏外部依赖栏下找到已经添加的依赖。【版本仲裁——maven就近优先原则】
    在这里插入图片描述
	<!--
	|<properties>
    |    <mysql.version>5.1.43</mysql.version>
    |</properties>
    -->
	<!--以上是B站视频中的方法,没有成功-->
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>

在这里插入图片描述
在这里插入图片描述

自动配置

默认的包结构

  • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
  • 想要改变扫描路径,@SpringBootApplication(scanBasePackages=“com.atguigu”) 或者@ComponentScan 指定扫描路径

@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(“com.atguigu.boot”)

容器功能

组件添加
  1. @Configuration

Full模式和Lite模式
配置 类组件之间无依赖关系用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")//此处组件名为自定义的“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@1654a892
        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));
    }
}
  1. @Bean、@Component、@Controller、@Service、@Repository

容器中注册组件
@Bean:组件
@Component:组件
@Controller:控制器
@Service:业务逻辑组件
@Repository:数据库组件

  1. @Import
/*  
*  @Import({User.class, DBHelper.class})
*    给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
*/
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}
  1. @Conditional

条件装配:满足Conditional指定的条件,则进行组件注入
在这里插入图片描述

=====================测试条件装配==========================
//@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);
    }
配置绑定
  1. 法一:@Component+@ConfigurationProperties
  • Car.java
/**
 * 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
 */
@Component //将Car加在容器中
@ConfigurationProperties(prefix = "mycar") //对应配置文件中的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 +
                '}';
    }
}
  • application.properties
mycar.brand=BYD
mycar.price=100000
#为汽车组件配置了两个属性
  • HelloController.java
@RestController //表明这个类(控制器)专门用来处理请求
public class HelloController {

    @Autowired
    Car car;
    @RequestMapping("/car")
    public Car car(){
        return car;
    }
}
  1. 法二:@EnableConfigurationProperties + @ConfigurationProperties
  • MyConfig.java(此时Car.java中不再标注@Component)
@EnableConfigurationProperties(Car.class)
//1.开启Car配置绑定功能
//2.把Car这个组件自动注册到容器中(代替Car类的@Component)
public class MyConfig {
}
  1. 两种方法的选择
    目前看来,法二更普适,因为如果引入第三方jar包中的类作为组件,用法二就避免在其文件中添加@Component。
其他标注
  • @AliasFor(“xxx”):别名xxx
  • @Autowired:将容器中的 bean 自动的和我们需要这个 bean 的类组装在一起
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值