zoukankan      html  css  js  c++  java
  • spring ConfigurationProperties 注解

    使用  @ConfigurationProperties 读取配置文件中的属性。项目为spring boot项目 

    项目依赖:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.2.7.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    	<groupId>com.test.demo</groupId>
    	<artifactId>demo</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>demo</name>
    	<description>Demo project for Spring Boot</description>
    
    	<properties>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-configuration-processor</artifactId>
    		</dependency>
    		
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    			<exclusions>
    				<exclusion>
    					<groupId>org.junit.vintage</groupId>
    					<artifactId>junit-vintage-engine</artifactId>
    				</exclusion>
    			</exclusions>
    		</dependency>
    
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>RELEASE</version>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    				<configuration>
    					<executable>true</executable>
    					<fork>true</fork>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>2.3.2</version>
    				<configuration>
    					<source>${java.version}</source>
    					<target>${java.version}</target>
    				</configuration>
    			</plugin>
    
    			<plugin>
    				<groupId>org.mybatis.generator</groupId>
    				<artifactId>mybatis-generator-maven-plugin</artifactId>
    				<version>1.3.2</version>
    				<configuration>
    					<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
    					<overwrite>true</overwrite>
    					<verbose>true</verbose>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    
    </project>
    

      

    默认application.properties

    hm.cloud.mycloud.user.test = test
    
    hm.cloud.mycloud.vmware.vm = vm
    

    通用properties配置类

    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "hm.cloud.mycloud")
    public class CommonProperties {
      // 这里需要注意 属性的名称一定要与配置文件(application.properties)中的名称保持一致
        private UserProperties user = new UserProperties();
    
        private VmwareProperties vmware = new VmwareProperties();
    
    
        public VmwareProperties getVmware() {
            return vmware;
        }
    
        public void setVmware(VmwareProperties vmware) {
            this.vmware = vmware;
        }
    
        public UserProperties getUser() {
            return user;
        }
    
        public void setUser(UserProperties user) {
            this.user = user;
        }
    }
    

      

    分子配置类,这里给了user 和 vmware两个配置类

    userproperties配置

    public class UserProperties {
        private String test;
    
        public String getTest() {
            return test;
        }
    
        public void setTest(String test) {
            this.test = test;
        }
    }

    VMware配置类

    public class VmwareProperties {
        private String vm;
    
        public String getVm() {
            return vm;
        }
    
        public void setVm(String vm) {
            this.vm = vm;
        }
    }
    

      

    核心配置类(spring的configuration)

    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableConfigurationProperties(CommonProperties.class)// 将自己的类引入
    public class PropertiesCoreConfig {
    }
    

    测试类:这里用一个controller来测试

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/properties")
    public class PropertiesTestController {
    
        @Autowired
        private CommonProperties commonProperties;
    
        @GetMapping
        public String propertiesTest(){
            String test = commonProperties.getUser().getTest();
            String vm = commonProperties.getVmware().getVm();
            System.out.println(test);
            System.out.println(vm);
            return "success";
        }
    }
    

      

    总结:大家可根据自己的项目 自行配置各个子类的层级,如上述例子中

    配置:

    hm.cloud.mycloud.user.test = test

    hm.cloud.mycloud.vmware.vm = vm

    将hm.cloud.mycloud作为一个公共的properties类

    user和VMware分别拆成两个单独的properties类。

  • 相关阅读:
    java 封装练习题3
    java 封装练习题2
    java 封装练习题1
    java 面向对象练习题6
    java 面向对象练习题5
    java 面向对象练习题4
    java 练习 计算5的阶乘 5!的结果是?
    java 练习 题目四:控制台输出九九乘法表
    java 练习 题目三:这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
    java 练习 题目二:我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的:5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
  • 原文地址:https://www.cnblogs.com/HanShisi/p/13273312.html
Copyright © 2011-2022 走看看