• SpringBoot底层注解总结


    @Configuration与@Import

    1. @Import({User.class, DBHelper.class})
    2. @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
    3. public class MyConfig {
    4. }

    @Configuration:告诉SpringBoot这是一个配置类,等同于 配置文件

    @Import({User.class, DBHelper.class})给容器中自动创建出这两个类型的组件、默认组件名字是全类名

    @Conditional条件装配

    1. @Configuration(proxyBeanMethods = false)
    2. @ConditionalOnMissingBean(name = "tom")//没有tom名字的Bean时,MyConfig类的Bean才能生效。
    3. public class MyConfig {
    4. }

    条件装配:满足Conditional指定的条件,才进行组件注入  

    没有tom名字的Bean时,MyConfig类的Bean才能生效。

    @ImportResource导入Spring配置文件

    若项目原来用bean.xml文件生成配置bean,想继续复用bean.xml,可用@ImportResource

    bean.xml:

    1. "1.0" encoding="UTF-8"?>
    2. <beans ...">
    3. <bean id="haha" class="com.yhd.boot.bean.User">
    4. <property name="name" value="liming">property>
    5. <property name="age" value="18">property>
    6. bean>
    7. beans>

    使用方法:

    1. @ImportResource("classpath:beans.xml")
    2. public class MyConfig {
    3. ...
    4. }

    @ConfigurationProperties配置绑定

    使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用

    传统方法:

    1. public class getProperties {
    2. public static void main(String[] args) throws FileNotFoundException, IOException {
    3. Properties pps = new Properties();
    4. pps.load(new FileInputStream("a.properties"));
    5. Enumeration enum = pps.propertyNames();//得到配置文件的名字
    6. while(enum.hasMoreElements()) {
    7. String strKey = (String) enum1.nextElement();
    8. String strValue = pps.getProperty(strKey);
    9. System.out.println(strKey + "=" + strValue);
    10. //封装到JavaBean
    11. }
    12. }
    13. }

    Spring Boot配置绑定方法:

    第一种:@ConfigurationProperties + @Component

    有配置文件application.properties:

    1. mycar.brand=BYD
    2. mycar.price=100000
    1. @Component
    2. @ConfigurationProperties(prefix = "mycar")
    3. public class Car {
    4. ...
    5. }

    第二种:@EnableConfigurationProperties + @ConfigurationProperties

            开启Car配置绑定功能

    1. @EnableConfigurationProperties(Car.class)
    2. public class MyConfig {
    3. ...
    4. }

            把这个Car这个组件自动注册到容器中

    1. @ConfigurationProperties(prefix = "mycar")
    2. public class Car {
    3. ...
    4. }

  • 相关阅读:
    QLExpress代码解读,运行原理解析
    【OpenCV】基于opencv的视频间隔抽帧脚本
    23种设计模式详解
    win10 Baichuan2-7B-Chat-4bits 上部署 百川2-7B-对话模型-4bits量化版
    【夜读】影响一生的五大定律内心强大的人,有这五种特质
    Linux系统下的Swift与Ceph分布式存储解决方案
    electron项目开机自启动
    设置单击右键可以选择用VS Code打开文件
    量子计算(十一):常见逻辑门以及含义
    RCC目前最近技术与今后发展
  • 原文地址:https://blog.csdn.net/m0_38071129/article/details/126867135