唐僧喜欢小龙女

导航

Java Bean中的属性和配置文件中的值进行绑定(@ConfigurationProperties的使用)

1、使用@ConfigurationProperties+@Component 组合注解

/**
 *
 *  ConfigurationProperties 翻译过来叫配置属性
 *  必须把Car放到容器中,属性配置才能生效,我这里使用的是@Import
 *
 */
@ConfigurationProperties(prefix = "mycar")
public class Car {

    private String brand;
    private int price;

    public String getBrand() {
        return brand;
    }

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

    public int getPrice() {
        return price;
    }

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


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

1.1、测试

@SpringBootApplication
@ImportResource("classpath:META-INF/spring/*.xml")
public class ProviderApplication {
    public static void main(String[] args) {
        //1、返回我们的IOC容器
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(ProviderApplication.class, args);

        System.err.println(configurableApplicationContext.containsBean("tomcatPet"));

        Car car = (Car) configurableApplicationContext.getBean(Car.class);
        System.out.println(car);


    }
}

2、 @EnableConfigurationProperties + @ConfigurationProperties 组合注解

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

/**
 *
 *  ConfigurationProperties 翻译过来叫配置属性
 *  必须把Car放到容器中,属性配置才能生效,我这里使用的是@Import
 *
 */
@ConfigurationProperties(prefix = "mycar")
public class Car {

    private String brand;
    private int price;

    public String getBrand() {
        return brand;
    }

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

    public int getPrice() {
        return price;
    }

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


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



/**
 * @EnableConfigurationProperties 作用
 * 1、开启属性配置功能,给Car 这个类开启实属性配置功能
 * 2、把Car 放到容器里面
 */
@EnableConfigurationProperties(Car.class)
@Configuration
public class MyCarConfig {
}

2.1 测试

@SpringBootApplication
@ImportResource("classpath:META-INF/spring/*.xml")
public class ProviderApplication {
    public static void main(String[] args) {
        //1、返回我们的IOC容器
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(ProviderApplication.class, args);

        System.err.println(configurableApplicationContext.containsBean("tomcatPet"));

        Car car = (Car) configurableApplicationContext.getBean(Car.class);
        System.out.println(car);


    }
}

3、特别说明

@ConfigurationProperties 这个注解在SpringBoot中很常见,这就要求我们配置某个属性的时候,不能随便写,必须按照SpringBoot的约定来,例如Server.port,不能写成Server1.port
否则自定义的端口就不生效了

posted on 2022-01-04 11:07  与时具进&不忘初心  阅读(369)  评论(0编辑  收藏  举报