06-SpringBoot配置绑定
1.5 注解@ConfigurationProperties进行组件属性和资源配置文件中的值进行绑定
-
注解@ConfigurationProperties+@Component的方式将application.properties文件中的值绑定到某个组件的属性上。注意现在默认只能是绑定核心资源配置文件中的标签的值
1、在项目的bean包中新建一个Car类,其中有两个属性:String brand和Integer price
2、在application.properties核心资源配置文件中自定义两个标签,并赋上值,如下
#自定义标签:前缀.后缀=值 #前缀:表示的是这一类标签,可以是有具体意义的,如这是要绑定到Car组件实例中的属性,可以把前缀 #定义为组件类名的首字母小写,当然也可以是多级的前缀xxx.zzz.yyy=ooo,主要用于过多使用该标签 #的组件时多层次区分 #后缀:就是要把标签的值绑定到组件中的属性名 mycar.brand=Ford mycar.price=10253
3、在Car类中添加注解。如下
/** * @ConfigurationProperties: * 表示将这个类的属性和核心资源配置文件中的标签值绑定 * 或者说就是给这个类的属性进行赋值、注入 * 属性: * prefix:表示前缀,填的是文件中标签的前缀 * 标签中的后缀不用操作,它会自动匹配赋值 * 这个注解的属性资源配置绑定只能和容器中的组件进行绑定,所以要将该类进行组件 * 注册,这里使用@Component */ @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 + '}'; } }
4、在主程序类中进行属性值查看,绑定成功
@SpringBootApplication public class BSpringbootAnnotationApplication { public static void main(String[] args) { //获取容器对象 ConfigurableApplicationContext run = SpringApplication.run(BSpringbootAnnotationApplication.class, args); //测试第一种资源配置绑定 System.out.println("组件car的属性:"+run.getBean("car")); //组件car的属性:Car{brand='Ford', price=10253} } }
-
注解@ConfigurationProperties+@EnableConfigurationProperties的方式将application.properties文件中的值绑定到某个组件的属性上。注意现在默认只能是绑定核心资源配置文件中的标签的值。这种方式解决的是你可能引入的是第三方的类,当需要绑定时,这个类不一定有@Component,但你又不能在原始类上添加的情况
1、Car类中去掉@Component注解
2、在配置类的类名上添加@EnableConfigurationProperties,其中一个属性是class类型的,需要我们填与绑定相关的类的Class对象,具体如下
/** * @EnableConfigurationProperties:翻译过来就是可进行属性资源绑定 * 作用: * 1、开启指定Class的配置绑定功能,这里是Car * 2、同时将Car这个类的对象作为组件放进容器中 */ @Configuration @EnableConfigurationProperties({Car.class})//第三方Car组件要进行绑定,把其放进容器中 public class MyConfig2 { /** * @ConditionalOnBean:条件装配注解 * 装配条件是容器中必须有该注解指定的组件 * (可以只用String集合name指定,也可以联合Class集合value指定,或者String集合的type来指定)才会有装配的效果 */ @ConditionalOnBean(name = "tomcat2",value = Pet.class) @Bean public User user02(){ User user = new User("李四"); return user; } //@Bean 让容器中不创建tomcat2的Pet组件 public Pet tomcat2(){ Pet pet = new Pet("tomcat2"); return pet; } }
然后在主程序类中测试,也实现了配置绑定,但是@EnableConfigurationProperties帮我们注册的Car实例组件在容器中的id是这样的,如下:
@SpringBootApplication public class BSpringbootAnnotationApplication { public static void main(String[] args) { //获取容器对象 ConfigurableApplicationContext run = SpringApplication.run(BSpringbootAnnotationApplication.class, args); //测试第一种资源配置绑定 //System.out.println("组件car的属性:"+run.getBean("car"));//组件car的属性:Car{brand='Ford', price=10253} //测试第二种资源配置绑定 String[] car = run.getBeanNamesForType(Car.class); System.out.println("@EnableConfigurationProperties帮我们创建的Car组件id:"+car[0]); //mycar-com.studymyself.bean.Car System.out.println("组件car的属性:"+run.getBean("mycar-com.studymyself.bean.Car")); //组件car的属性:Car{brand='Ford', price=10253} } } 结果: @EnableConfigurationProperties帮我们创建的Car组件id:mycar-com.studymyself.bean.Car 组件car的属性:Car{brand='Ford', price=10253}