Spring boot 自动配置自定义配置文件

 

 

示例如下:

1.   新建 Maven 项目 properties

 

2.   pom.xml

<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 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java</groupId>
    <artifactId>properties</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>


    <dependencies>

        <!-- Spring Boot -->
        <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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>


        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

 

3.   配置文件  user-defined.properties

aaa.url.login=aaa-login.html
aaa.url.order=aaa-order.html

bbb.goods.price=500
bbb.goods.weight=1000

 

4.   PropertiesStarter.java

package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 主启动类
 * 
 * @author Storm
 *
 */
@SpringBootApplication
public class PropertiesStarter {

    public static void main(String[] args) {
        SpringApplication.run(PropertiesStarter.class, args);
    }

}

 

5.   URLProperties.java

package com.java.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

/**
 * 前缀为aaa.url的配置自动注入到对应属性中
 * 
 * @author Storm
 *
 */
@Data
@ConfigurationProperties(prefix = "aaa.url")
public class URLProperties {

    private String login;
    private String order;

}

 

6.   GoodsProperties.java

package com.java.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

/**
 * 前缀为bbb.goods的配置自动注入到对应属性中
 * 
 * @author Storm
 *
 */
@Data
@ConfigurationProperties(prefix = "bbb.goods")
public class GoodsProperties {

    private String price;
    private String weight;

}

 

7.   PropertiesConfig.java

package com.java.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.java.properties.GoodsProperties;
import com.java.properties.URLProperties;

/**
 * 用户自定义配置文件配置类
 * 
 * @author Storm
 *
 */
@Configuration
@PropertySource({ "classpath:user-defined.properties" })
@EnableConfigurationProperties({ URLProperties.class, GoodsProperties.class })
public class PropertiesConfig {

}

 

8.   DemoController.java

package com.java.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.properties.GoodsProperties;
import com.java.properties.URLProperties;

@RestController
public class DemoController {

    @Autowired
    private URLProperties url;

    @Autowired
    private GoodsProperties goods;

    @GetMapping("/getProperties")
    public Map<String, Object> getProperties() {

        System.out.println(url.getOrder());

        Map<String, Object> map = new HashMap<>();
        map.put("url", url);
        map.put("goods", goods);
        return map;
    }

}

 

9.   运行 PropertiesStarter.java ,启动测试

浏览器输入 http://localhost:8080/getProperties

返回结果如下:

{"goods":{"price":"500","weight":"1000"},"url":{"login":"aaa-login.html","order":"aaa-order.html"}}

 

 

 

配置加载成功!

 

 

 

Spring boot 自动配置自定义配置文件

.

posted @ 2019-01-28 20:22  诚信天下  阅读(1078)  评论(0编辑  收藏  举报