Springboot配置绑定和条件装配
其他配置注解
- @ConditionalOnBean(name = “tom”) // 条件装配 ,存在tom容器时,配置类中所有容器才会生效。
- @ConditionalOnMissingBean(name = “tom”) // 和上个注解相反
- @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 {@Beanpublic 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;}@Overridepublic 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 {@Beanpublic User user(){return new User("toec",20);}@Bean("tom")public Pet pet(){return new Pet("dog");}
}
mycar.brand=BYD
mycar.price=10000
Springboot配置绑定和条件装配相关推荐
- SpringBoot配置绑定的两种方式
SpringBoot配置绑定的两种方式 演示文件 bean public class Student {private String name;private Integer age;public S ...
- SpringBoot中Bean按条件装配
@Conditional条件装配 @Conditional是Spring Framework提供的一个核心功能注解,这个注解的作用是提供自动装配的条件限制,一般我们在用@Configuration,@ ...
- SpringBoot 配置绑定
一.如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用 public class getProperties {public static void ...
- SpringBoot 中的 3 种条件装配!
一.介绍 在实际的项目开发中,我们往往需要根据不同的环境做出不同的配置,例如:在开发环境下,我们会使用内存数据库以便快速启动服务并进行开发调试,在test环境.生产环境,会使用对应环境的数据库. 如果 ...
- SpringBoot复习:5(配置绑定)
配置绑定 在SpringBoot中我们如何读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用呢? 我们可以使用以下两种方式来实现. 方法一:@Configurati ...
- SpringBoot底层注解-@ConfigurationProperties配置绑定
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用: public class getProperties {public static void ...
- @scheduled注解配置时间_「玩转SpringBoot」用好条件相关注解,开启自动配置之门...
封面图自动配置隐含两层含义,要搞清楚 上帝让程序员的发量减少,是为了让他变得更聪明,如果有一天聪明到了极点,那就是绝顶聪明. 据说在大脑高速运转下,这样更有利于散热,不至于核心温度过高而产生告警. 聪 ...
- SpringBoot的配置绑定
了解自动配置绑定原理! 结构如下: 在MainApplication同级中创建一个包,包下文件名为:Person.java package com.jin.boot.bean;import org.s ...
- [SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource
@Configuration&@Bean 告诉SpringBoot这是一个配置类==配置文件 #############################Configuration使用示例### ...
最新文章
- TCP/IP 协议栈及 OSI 参考模型详解--云平台技术栈04
- EasyExcel读取文件异常,报 java.lang.NoClassDefFoundError,/x2006/main/CTTableStyles
- 七天开发安卓软件(五)
- 计算机系统 过程调用
- 罗马数字 java_【leetcode刷题】[简单]13.罗马数字转整数(roman to integer)-java
- Linux 命令之 find -- 查找文件和目录/搜索文件和目录
- hdu5468 Puzzled Elena
- SAGAN(G-lab介绍)
- 如何设置input表单根据表单内的数据自动增高_「第三天」学美术的英语小白如何从装修行业转为互联网前端...
- 初了解JS设计模式,学习笔记
- 集成maven和Spring boot的profile 专题
- blowfish java_Java语言实现Blowfish加密算法完整代码分享
- 2022下半年软考报名入口!
- Ubuntu18.04与RTX1080Ti安装深度学习框架
- 计算机基础知识----课后习题
- python查看某个函数方法具体有哪些参数怎么做
- 如何用研发效能搞垮一个团队
- oracle的system账户用normal模式登录失败,提示用户名或密码错误,但是如果用sysdba登录,即使不输入密码,或者密码错误也能登录成功。
- Google 不想与甲骨文争了,要放弃 Java APIs
- k8s高可用多节点master搭建