Java生鲜电商平台-SpringBoot2自动获取配置文件?(小程序/APP)

Java生鲜电商平台-SpringBoot2自动获取配置文件?(小程序/APP) 

说明:在Java生鲜电商平台中,有些代码是可以写活的,有些是可以写死的,写活的方式有可以写DB,然后加载缓存,有的可以写.properties文件或者yml文件进行读取,今天我简单

写下从配置文件加载的方式,代码全部贴出来..

从本文你可以学习到的东西

1,springboot2中的@PropertySource注解,以及@ConfigurationProperties注解的使用。具体基础知识,自己去百度。

1. 新建一个RandomConfig这个类:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Java生鲜电商
 */
@Component
@ConfigurationProperties(prefix = "user.random")
@PropertySource(value = {"random.properties"})
@Data
public class RandomConfig {

    private String secret;   //#随机32位MD5字符串
    private int intNumber;   //随机int数字
    private int lessTen;     //##随机10以内的数字
    private int range;       //#随机1024~65536之内的数字
    private long longNumber; //#随机long数字
private String uuid; //随机UUID
}

说明:

@Componentm,@Data注解不讲
@PropertySource 配置资源文件的名称以及地址,默认是属于classes的src下面。
@ConfigurationProperties 配置内容的前缀

下面贴出:
random.properties的内容
#随机32位MD5字符串
user.random.secret=${random.value}

#随机int数字
user.random.intNumber=${random.int}

#随机long数字
user.random.longNumber=${random.long}

#随机uuid
user.random.uuid=${random.uuid}

#随机10以内的数字
user.random.lessTen=${random.int(10)}

#随机1024~65536之内的数字
user.random.range=${random.int[1024,65536]}

3. 使用方式

package cn.javastack.springboot.random;

import cn.javastack.springboot.random.config.RandomConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * Java生鲜电商
 */
@SpringBootApplication
@EnableConfigurationProperties
public class Application {

    @Autowired
    private RandomConfig randomConfig;

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

    @Bean
    public CommandLineRunner commandLineRunner() {
        return (args) -> {
            System.out.println(randomConfig);
        };
    }

}
值得注意的是:@EnableConfigurationProperties,这个注解需要加上,不然无法起作用.

POM文件也贴出来,可以参考:

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
原文地址:https://www.cnblogs.com/jurendage/p/14310356.html