码迷,mamicode.com
首页 > 编程语言 > 详细

Spring boot 引用自定义配置文件

时间:2019-01-28 21:10:50      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:group   配置   自定义   port   string   storm   factor   加载   注入   

 

依赖jar

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.0.5.RELEASE</version>
</dependency>

 

 

示例如下:

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 引用自定义配置文件

标签:group   配置   自定义   port   string   storm   factor   加载   注入   

原文地址:https://www.cnblogs.com/jonban/p/properties.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!