【SpringBoot02】学习笔记--自动配置、注解@Configuration@Import@Conditional@ImportResource @ConfigurationProperties

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这个组件自动注册到容器中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,@Conditional注解和@AutoConfigureAfter注解是非常常用的注解,下面我来给你详细解析一下这两个注解。 ## @Conditional注解 @Conditional注解Spring Boot中非常重要的一个注解,在Spring Boot中,很多自动配置都是通过@Conditional注解来实现的。 @Conditional注解可以根据满足某些条件来决定是否创建一个bean。比如,我们可以根据某个是否存在来决定是否创建一个bean,具体示例如下: ```java @Configuration @Conditional(ExistClassCondition.class) public class MyConfiguration { @Bean public MyBean myBean() { return new MyBean(); } } public class ExistClassCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { try { Class.forName("com.example.MyClass"); return true; } catch (ClassNotFoundException e) { return false; } } } ``` 上面的代码中,我们定义了一个MyConfiguration,并且在该上加了@Conditional注解,该注解的参数是一个Condition的实现ExistClassCondition。ExistClassCondition中的matches方法返回true的条件是com.example.MyClass存在。 这样,当com.example.MyClass存在的时候,MyBean这个bean才会被创建。否则,MyBean这个bean不会被创建。 ## @AutoConfigureAfter注解 @AutoConfigureAfter注解也是Spring Boot中比较常用的注解之一,它可以用来控制自动配置的顺序。 比如,我们可以通过@AutoConfigureAfter注解来控制某个自动配置在另一个自动配置之后加载,具体示例如下: ```java @Configuration @AutoConfigureAfter(MyAutoConfiguration.class) public class MyAnotherAutoConfiguration { // ... } ``` 上面的代码中,我们定义了一个MyAnotherAutoConfiguration,并且在该上加了@AutoConfigureAfter注解,该注解的参数是MyAutoConfiguration.class。这样,在Spring Boot启动时,MyAutoConfiguration这个自动配置会先于MyAnotherAutoConfiguration这个自动配置被加载。 总结:@Conditional注解和@AutoConfigureAfter注解都是Spring Boot中非常实用的注解。通过@Conditional注解可以实现根据满足某些条件来决定是否创建一个bean,通过@AutoConfigureAfter注解可以控制自动配置的加载顺序,这些都是我们在实际开发中非常常用的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值