Spring Boot 读取应用配置

Spring Boot 读取应用配置

Spring Boot提供了三种方式读取项目application.properties配置文件的内容,这三种方式分别是Eviroment类,@Value注解,@ConfigurationProperties注解。

1-使用Enviroment类读取配置文件的内容。
(1)创建Spring Boot Web应用。
(2)在src/main/resources目录下创建全局配置文件application.properties,并添加如下内容:

test.msg = read config

(3)创建控制器类,在src/main/java目录下创建包com.controller,在该包中创建控制器类,依赖注入Enviroment对象,通过对象的getProperty()方法获取配置文件中内容。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EnvReaderConfigController {
    @Autowired
    private Environment env ;
    @RequestMapping("/testEnv")
    public String testEnv(){
        //test.msg为配置文件application.properties中的key
        return "方法1 : " + env.getProperty("test.msg") ;
    }
}

(4)在com.controller包中创建Spring Boot的启动类。并运行该启动类。

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

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

(5)在浏览器中通过地址:http://localhost:8080/testEnv测试应用,效果如下:
在这里插入图片描述
2-使用@Value注解读取配置文件内容
(1)创建Spring Boot Web应用。
(2)在src/main/resources目录下创建全局配置文件application.properties,并添加如下内容:

test.msg = read config

(3)在com.controller包下创建控制器类。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ValueReaderConfigController {
    @Value("${test.msg}")
    private String msg ; //读取配置文件的内容并传递给msg
    @RequestMapping("/testValue")
    public String testValue(){
        return "方法2 : " + msg ;
    }
}

(4)在com.controller包中创建Spring Boot的启动类。并运行该启动类。

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

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

(5)测试应用,在浏览器中通过地址:http://localhost:8080/testValue测试应用,效果如下:
在这里插入图片描述
3-使用@ConfigurationProperties读取配置文件内容。
使用该注解首先建立配置文件与对象的映射关系,然后在控制器方法中使用@Autowired注解将对象注入。
(1)创建Spring Boot web项目,并在全局配置文件application.properties中添加如下内容:

obj.name = wangguodong
obj.age = 25

(2)建立配置文件与映射对象的关系,在src/main/java目录下创建com.model包,并在该包中创建实体类StudentProperties,在该类中使用@ConfigurationProperties注解建立配置文件与对象的映射关系。

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

@Component //声明为一个组件,被控制器依赖注入
@ConfigurationProperties(prefix = "obj")
public class StudentProperties {
    private String name ;
    private int age ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public String toString(){
        return "name = " + name + "  age = " + age  ;
    }
}

(3)在com.controller包创建控制器类,在该控制器中使用@Autowired注解依赖注入StudentProperties对象,代码如下:


import com.model.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigurationPropertiesController {
    @Autowired
    StudentProperties studentProperties ;
    @RequestMapping("/testConfigurationProperties")
    public String testConfigurationProperties(){
        return studentProperties.toString() ;
    }
}

(4)在com.controller中创建启动类,并指定要扫描的包。

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

@SpringBootApplication(scanBasePackages = {"com"})
public class SpringBoot11Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringBoot11Application.class, args) ;
    }
}

(5)运行main方法,启动并测试应用,并在浏览器地址:http://localhost:8080/testConfigurationProperties中访问,效果如下:
在这里插入图片描述
4-读取项目的其它配置文件
开发者希望读取项目的其它配置文件,而不是全局配置文件application.properties,可以使用@PropertySource注解找到其它配置文件,然后使用@value注解读取即可。

(1)在src/main/java目录下创建配置文件ok.properties和test.properties,并在两个配置文件中分别添加如下内容:

your.msg = I love you
my.msg = me too

(2)创建控制器类,在该控制类中使用@PropertySource注解找到其它配置文件,然后使用@value注解读取配置文件。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@PropertySource({"ok.properties", "test.properties"})
public class PropertySourceValueReaderOtherController {
    @Value("${your.msg}")
    private String yourMsg  ;
    @Value("${my.msg}")
    private String myMsg ;
    @RequestMapping("/testProperties")
    public String testProperty(){
        return "您的信息:" + yourMsg + "  我的信息:" + myMsg ;
    }
}

(3)在com.controller中创建启动类,并指定要扫描的包。

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

@SpringBootApplication(scanBasePackages = {"com"})
public class SpringBoot11Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringBoot11Application.class, args) ;
    }
}

(4)运行main方法,启动并测试应用,并在浏览器地址:http://localhost:8080//testProperties中访问,效果如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值