栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 学习笔记02之依赖管理特性与底层注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 学习笔记02之依赖管理特性与底层注解

Learn from:尚硅谷

依赖管理特性

pom中的父依赖的父项目为


    org.springframework.boot
    spring-boot-dependencies
    2.5.6
  

文件内声明了很多依赖的版本仲裁,这样配置的适合就不需要写版本号了

例如需要使用mysql的依赖,按照下面添加:

		
            mysql
            mysql-connector-java
        

如果需要自己决定版本号,可以去父项目中查看该依赖的名称按照下面的格式选择:

    
        5.1.43
    

自动配置tomcat

我们可以发现tomcat已经在我们需要的jar包中了。SpringBoot帮助我们配置好了很多的场景

自动配好SpringMVC和Web开发的功能 默认的包结构

主程序下的程序和包下的程序都会扫描出。
如果想要改变扫描结构。可以使用:

@SpringBootApplication(scanbasePackages="com.atguigu")
@ComponentScan(scanbasePackages="com.atguigu")

@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
  
配置的默认值

各种配置都有一个默认值,并且每个配置都对应了一个java类

@Configuration的Full模式与Lite模式 Full模式
@Configuration(proxyBeanMethods = true)

当代理bean方法为true的时候,他就会对配置类组件进行一个代理,组件包括当前类。
当代理的时候,为了保证容器中只有一个单实例组件,每次创建实例前都会检查一次是否创建了实例。
这样每次检查一次消耗了时间,但节省了内存开支。

且如果配置类组件间有依赖关系的时候,调用方法会调用之前生成的单实例组件。因此含有依赖关系的配置类,必须使用Full模式。

Lite模式
@Configuration(proxyBeanMethods = false)

当代理bean方法为false的时候,为lite模式。当前模式在创建实例的时候不会检查容器中是否有之前创建的实例,因此lite模式的速度快一些。但是生成的实例多,消耗的内存相对会多一些。

如果配置类组件间无依赖关系,使用lite模式可以减少判断,提高程序运行的速度。

测试

现在代码中为true模式,测试false需要修改proxyBeanMethod为false

package com.levia.boot;

import com.levia.boot.bean.Pet;
import com.levia.boot.bean.User;
import com.levia.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);
       //  1、返回我们的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);


        //2、查看组件里的容器
        String[] beanDefinitionNames = run.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
                System.out.println(beanDefinitionName);
        }

        //3、从容器中获取组件
        Pet myTomCat01 = run.getBean("MyTomCat", Pet.class);

        Pet myTomCat02 = run.getBean("MyTomCat", Pet.class);

        MyConfig bean = run.getBean(MyConfig.class);


        System.out.println("组件:"+(myTomCat01 == myTomCat02));

        System.out.println(bean);

        // 如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件
        //是否在容器中存在,如果有,它会保证组件单实例
        User user01 = bean.user01();

        User user02 = bean.user01();
        System.out.println(user01==user02);


        User user011 = run.getBean("user01", User.class);
        Pet tomcat = run.getBean("MyTomCat", Pet.class);
        System.out.println("用户的宠物:"+(user011.getPet()==tomcat));


    }
}

package com.levia.boot.config;

import com.levia.boot.bean.Pet;
import com.levia.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.jws.soap.SOAPBinding;


@Configuration(proxyBeanMethods = true) //这个是配置类
public class MyConfig {

    

    @Bean // 给容器添加组件 方法名作为组件id,返回类型就是组件类型,返回的值就是组件在容器中的实例
    public User user01(){
        User zhangsan = new User("zhangsan",17);
        // user组件依赖了pet组件
        zhangsan.setPet(tomcat());
        return zhangsan;
    }

    @Bean("MyTomCat") // 自定义组件名
    public Pet tomcat(){
        return new Pet("tomcat",6);
    }

}

Full模式
代表两个组件是相同的一个实例
且MyConfig类是一个被代理的类
但是下面新生成的两个cat也是相同的,代表代码进行了判断,没有额外的实例生成

如果为lite模式
组件只创建了一次,因此是相同的,但是MyConfig类没有被代理,且这时候实例化的时候是创建了两个不同的组件

因此返回为false,下面的cat不相同也是这样。因为配置类组件方法中新声明的pet不是原来声明的,为多实例。

@import

该注解的参数为一个数组,也就是需要导入组件的名字全都放入数组里面。

该注解的作用为自动创建组件,组件的名字为全类名。

运行主程序后发现控制台输出含有这两个组件

        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }

        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1);
@Conditional

该注解的作用为情景绑定
当有。。。的时候才装配。。。

类如下面的代码的意思是当没有tom这个组件的时候才开始装配

@ConditionalOnMissingBean(name = "tom")


运行主程序后发现含有user01和tom22,但不含有tom。

@importResource

如果之前的项目的组件是使用bean-xml来声明的,现在需要使用这些组件,我们需要在配置类上声明组件配置文件的文件名和位置,下面声明的就是在类路径下。

@importResource("classpath:beans.xml")


bean内为:


    
        

        
        

        
    

    
        

        
        

        
    

最后运行主程序,我们可以看到容器中含有hehe与haha。

        boolean hehe = run.containsBean("hehe");
        System.out.println("hehe:"+hehe);

        boolean haha = run.containsBean("haha");
        System.out.println("haha:"+haha);

@ConfigurationProperties

该注解的作用为将配置文件中的值传递到单实例组件中。

新建一个java类 Car

该类需要使用配置绑定生成单实例组件,并返回到网页中。因此,我们需要再给它添加一个**@Conponent**注解,该注解的作用为,将该类添加到组件中,并且是单实例组件。

package com.levia.boot.bean;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@Component //配置绑定
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;

    private Integer price;

    public Car(){

    }

    public Car(String brand,Integer price){
        this.brand=brand;
        this.price=price;
    }

    public Integer getPrice() {
        return price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + ''' +
                ", price=" + price +
                '}';
    }

}

在控制类中将该类返回到网页中

控制类
@Autowired注解的作用为将容器中的单实例自动注入

第二种方法

如果在原来的类种没有@Component注解,但是含有@ConfigurationProperties(prefix = “mycar”),我们可以在配置类种添加@EnableConfigurationProperties(Car.class),开启Car这个类的配置功能就可以使用了。

配置类

文件目录截图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号