spring注入配置文件中的变量 @PropertySource

直接上代码

这是配置文件的内容 配置文件的路径/resources/config/system.properties

#云平台超级管理员id
platformAdminId=1
#基础系统(后台管理系统)菜单id
baseSystemId=5

创建一个配置类

package com.github.wxiaoqi.security.admin.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Author: HuPengCheng
 * @Date: 2019/3/26 8:59
 */
@Component
@Configuration
@PropertySource("classpath:config/system.properties")
public class SystemConfig {

    @Value("${platformAdminId}")
    public String platformAdminId;

    @Value("${baseSystemId}")
    public String baseSystemId;

    @Bean
    public SystemConfig SystemConfig() {
        return new SystemConfig();
    }

    public String getPlatformAdminId() {
        return platformAdminId;
    }

    public void setPlatformAdminId(String platformAdminId) {
        this.platformAdminId = platformAdminId;
    }

    public String getBaseSystemId() {
        return baseSystemId;
    }

    public void setBaseSystemId(String baseSystemId) {
        this.baseSystemId = baseSystemId;
    }
}

调用方式

 @Autowired
    private SystemConfig systemConfig;

    @GetMapping("/menus/{groupId}")
    @ResponseBody
    public Object getMenus(@PathVariable Integer groupId){
        return systemConfig.baseSystemId;
    }

但是一般配置文件中的参数作为静态资源使用起来会比较便捷

修改配置类:

package com.github.wxiaoqi.security.admin.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Author: HuPengCheng
 * @Date: 2019/3/26 8:59
 */
@Component
@Configuration
@PropertySource("classpath:config/system.properties")
public class SystemConfig {


    public static String platformAdminId;


    public static String baseSystemId;

    public static String getPlatformAdminId() {
        return platformAdminId;
    }

    @Value("${platformAdminId}")
    public void setPlatformAdminId(String platformAdminId) {
        SystemConfig.platformAdminId = platformAdminId;
    }

    public static String getBaseSystemId() {
        return baseSystemId;
    }

    @Value("${baseSystemId}")
    public void setBaseSystemId(String baseSystemId) {
        SystemConfig.baseSystemId = baseSystemId;
    }
}

调用方式:直接类名.变量名调用即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值