直接上代码
这是配置文件的内容 配置文件的路径/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;
}
}
调用方式:直接类名.变量名调用即可