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类。

 

posted @ 2020-07-09 14:06  韩小鑫  阅读(486)  评论(0编辑  收藏  举报