Springboot配置绑定和条件装配

38 篇文章 0 订阅
2 篇文章 0 订阅
  1. 其他配置注解

    1. @ConditionalOnBean(name = “tom”) // 条件装配 ,存在tom容器时,配置类中所有容器才会生效。
    2. @ConditionalOnMissingBean(name = “tom”) // 和上个注解相反
    3. @ImportResource(“classpath:bean.xml”) // 导入xml容器配置资源
    package com.toec.boot.config;
    /**
     * @ProjectName: www-ES-Api
     * @Package: com.toec.boot.config
     * @ClassName: Config
     * @Author: 125827
     * @Description: bean配置
     * @Date: 1/9/2022 10:07 PM
     * @Version: 1.0
     */
    @Import({DBHelper.class,User.class}) //可以导入全类名组件
    @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类==配置文件
    @ConditionalOnBean(name = "tom")  //条件装配 ,存在tom容器时,配置类中所有容器都不会生效。
    // @ConditionalOnMissingBean(name = "tom") //和上个注解相反
    @ImportResource("classpath:bean.xml") //导入xml容器配置资源
    public class MyConfig {
        @Bean
        public User user(){
            return new User("toec",20);
        }
        @Bean("tom")
        public Pet pet(){
            return new Pet("dog");
        }
    
    }
    

2、配置绑定

  • @EnableConfigurationProperties(Car.class)
    // 1、开启car配置绑定功能
    // 2、把这个car这个组件自动注册到容器中
  • @ConfigurationProperties(prefix=“mycar”)
package com.toec.boot.bean;

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

/**
 * @Package: com.toec.boot.bean
 * @ClassName: Car
 * @Description: 汽车
 * @Date: 1/10/2022 12:35 AM
 * @Version: 1.0
 * ConfigurationProperties只有在容器中的组件,才会拥有SpringBoot提供的强大功能
 */
// @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 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 +
                '}';
    }
}
package com.toec.boot.config;

import ch.qos.logback.core.db.DBHelper;
import com.toec.boot.bean.Car;
import com.toec.boot.bean.Pet;
import com.toec.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/**
 * @Package: com.toec.boot.config
 * @ClassName: Config
 * @Description: bean配置
 * @Date: 1/9/2022 10:07 PM
 * @Version: 1.0
 */
@Import({DBHelper.class,User.class}) //可以导入全类名组件
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类==配置文件
//@ConditionalOnBean(name = "tom")  //条件装配
@ConditionalOnMissingBean(name = "tom") //和上个注解相反
//@ImportResource("classpath:bean.xml")
@EnableConfigurationProperties(Car.class)
//1、开启car配置绑定功能
//2、把这个car这个组件自动注册到容器中
public class MyConfig {
    @Bean
    public User user(){
        return new User("toec",20);
    }

    @Bean("tom")
    public Pet pet(){
        return new Pet("dog");
    }
}
mycar.brand=BYD
mycar.price=10000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脑袋不够用的小渣渣

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

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

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

打赏作者

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

抵扣说明:

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

余额充值