zoukankan      html  css  js  c++  java
  • @EnableConfigurationProperties + @ConfigurationProperties 注解

    @EnableConfigurationProperties + @ConfigurationProperties

    @EnableConfigurationProperties注解有两个功能:
    1.开启参数cat的配置绑定功能
    2.将参数cat自动注册到容器中,也就是说在cat的实体类中不需要加@Component注解
    注意:

    ​ 1.@ConfigurationProperties注解还是要加的;

    ​ 2.注解要添加在配置类上

    系统要求

    Java 8+

    Maven 3.6.6 +

    创建Maven项目工程

    引入 pom.xml 依赖
        <!--1.导入父工程-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.0</version>
        </parent>
    
        <!--2.导入springBoot的Web场景-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
     <build>
            <!--
            SpringBoot项目默认打成jar包
                传统web项目我们需要打成war包,放入tomcat中运行,springBoot项目我们可以导入一个插件,
                在项目打成jar包的同时,还会顺带打包运行环境,所以只要直接运行jar包也可以访问项目
            -->
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    创建实体类 pojo.Cat类
    package com.xiang.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/12 14:05
     */
    
    /**
     * 只有在容器中的组件,才会拥有SpringBoot提供的功能,也就是才可以使用注解
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    //@Component
    @ConfigurationProperties(prefix = "cat")
    //该注解表示:从application.properties中读取前缀为cat的数据赋值给下列属性,注意文件中的属性名和实体类中的属性名一致
    public class Cat {
        private  String name;
        private  double price;
    }
    
    
    application.properties文件
    #在配置文件中给Cat这个实例赋值
    cat.name="tom"
    cat.price=500.00
    
    
    新建 MyConfig 类
    package com.xiang.config;
    
    import com.xiang.pojo.Cat;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/12 15:42
     */
    @Configuration
    @EnableConfigurationProperties(Cat.class)
    //该注解有两个功能:
    //1.开启参数cat的配置绑定功能
    //2.将参数cat自动注册到容器中,也就是说在cat的实体类中不需要加@Component注解
    //注意:@ConfigurationProperties注解还是要加的
    public class MyConfig {
    }
    
    
    主程序进行测试 MainApplication 类
    package com.xiang;
    
    import com.xiang.pojo.Cat;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/12 14:35
     */
    @SpringBootApplication
    public class MainApplication {
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
    
    //        Cat cat = context.getBean("cat", Cat.class); //这个会报错---->: No bean named 'cat' available
            Cat cat = context.getBean("cat-com.xiang.pojo.Cat", Cat.class);
            System.out.println(cat);
            /**
             * Cat(name="tom", price=500.0)
             */
    
            System.out.println("/**************************************************/");
            System.out.println("遍历容器中的所有组件");
            //遍历容器中的所有组件
            //getBeanDefinitionNames():返回容器中的所有组件
            String[] names = context.getBeanDefinitionNames();
            for (String name : names) {
                System.out.println(name);
            }
            /**
             * cat-com.xiang.pojo.Cat
             */
        }
    }
    
    
    运行结果
    Cat(name="tom", price=500.0)
    /**************************************************/
    遍历容器中的所有组件
    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    org.springframework.context.annotation.internalCommonAnnotationProcessor
    org.springframework.context.event.internalEventListenerProcessor
    org.springframework.context.event.internalEventListenerFactory
    mainApplication
    org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
    myConfig
    org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
    org.springframework.boot.context.internalConfigurationPropertiesBinderFactory
    org.springframework.boot.context.internalConfigurationPropertiesBinder
    org.springframework.boot.context.properties.BoundConfigurationProperties
    org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter
    cat-com.xiang.pojo.Cat
    org.springframework.boot.autoconfigure.AutoConfigurationPackages
    ......
    

  • 相关阅读:
    [Abp vNext 源码分析]
    C# 结合 PInvoke 对接 IP 摄像头的笔记
    Abp vNext 自定义 Ef Core 仓储引发异常
    [Abp vNext 源码分析]
    [Abp vNext 源码分析]
    [Abp vNext 源码分析]
    网站SEO中服务器优化的三个问题
    用香港服务器还是国内服务器好
    租用香港服务器最重要的三个问题
    如何选择服务器操作系统
  • 原文地址:https://www.cnblogs.com/d534/p/15398266.html
Copyright © 2011-2022 走看看