http://blog.csdn.net/cloume/article/details/52538626

Spring Boot中使用自定义的properties

Spring Boot的application.properties中已经提供了很多默认的配置,如果要添加自定义的配置该怎么办呢?我们可以继续在application.properties中添加自定义的配置项,也可以使用新的配置文件,在里面添加自定义的配置项。比如说最近我在做一个简单的系统权限配置,我就把 
系统的角色和角色可操作的权限列表写在application.properties中以方便进行更新。那么怎么获取自定义的配置项内容呢?

1. 在application.properties中添加配置项

比如说我在application.properties中添加了如下配置项:

# user privilege
privilege.assistor=assitor
privilege.admin=assistor_create,star_operate,requirement_dispatch,fee_return,fee_charge,expiration_set
privilege.superman=admin_create
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

依然可以通过@Value注解方便地获取到配置项的值:

@Value("${privilege.assistor}") private String assistorPrivileges;
  • 1
  • 1

2. 使用新的配置文件

为了和其他默认配置项保持独立,我也可以新建一个privilege.properties的配置文件,然后将权限相关的配置内容放到这个文件里面:

privilege.properties

# user privilege
privilege.assistor=assitor
privilege.admin=assistor_create,star_operate,requirement_dispatch,fee_return,fee_charge,expiration_set
privilege.superman=admin_create
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这个时候再使用@Value注解就拿不到配置项的值了。怎么办呢?我们可以新增相应的配置项类,在需要使用到配置项的地方直接注入使用。

1. 定义配置类

package com.cloume.hsep.security;

import java.util.ArrayList;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.security.core.authority.SimpleGrantedAuthority; //privilege.properties是放在resource/config目录下的
@ConfigurationProperties(prefix = "privilege", locations = "classpath:application.properties")
public class PrivilegeSettings { private String superman;
private String assistor;
private String admin; private static ArrayList<SimpleGrantedAuthority> superPrivileges = new ArrayList<SimpleGrantedAuthority>();
private static ArrayList<SimpleGrantedAuthority> adminPrivileges = new ArrayList<SimpleGrantedAuthority>();
private static ArrayList<SimpleGrantedAuthority> assistorPrivileges = new ArrayList<SimpleGrantedAuthority>(); public String getSuperman() {
return superman;
} public void setSuperman(String superman) {
this.superman = superman;
for(String privilege : superman.split(",")){
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(privilege);
superPrivileges.add(authority);
}
} public String getAssistor() {
return assistor;
} public void setAssistor(String assistor) {
this.assistor = assistor;
for(String privilege : assistor.split(",")){
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(privilege);
assistorPrivileges.add(authority);
}
} public String getAdmin() {
return admin;
} public void setAdmin(String admin) {
this.admin = admin;
for(String privilege : admin.split(",")){
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(privilege);
adminPrivileges.add(authority);
}
} public ArrayList<SimpleGrantedAuthority> getSuperPrivileges() {
return superPrivileges;
} public ArrayList<SimpleGrantedAuthority> getAdminPrivileges() {
return adminPrivileges;
} public ArrayList<SimpleGrantedAuthority> getAssistorPrivileges() {
return assistorPrivileges;
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

2. 在Spring Boot入口类增加配置项注解@EnableConfigurationProperties

   @SpringBootApplication
@EnableConfigurationProperties({PrivilegeSettings.class, Privilege2Settings.class})
public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3. 使用自定义properties类

   @RestController
public class TestController { @Autowired
private PrivilegeSettings privilegeSettings; @RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody String test(){
System.out.println(privilegeSettings.getSuperman());
System.out.println(privilegeSettings.getAdmin());
System.out.println(privilegeSettings.getAssistor()); return "OK";
}
}
 
 
SpringBoot读取application.properties文件,通常有3种方式

1. @Value  例如:

@Value("${spring.profiles.active}")

private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中

2. @ConfigurationProperties  例如:

@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;

}

其他类中使用时,就可以直接注入该TestProperties 进行访问相关的值

3. 使用Enviroment   例如:

private Enviroment env;

env.getProperty("test.url");

而env方式效率较低

注:@ConfigurationProperties也可用于其他.properties文件,只要locations指定即可

SpringBoot读取application.properties文件的更多相关文章

  1. SpringBoot读取application.properties文件内容

    application.properties存储数据的方式是key-value. application.properties内容 userManager.userFile=data/user.pro ...

  2. SpringBoot读取application.properties中文乱码

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 解决方案 在ID ...

  3. springboot读取application.properties中自定义配置

    假设在application-xxx.properties中配置 user.name=yuhk 一.在Controller中读取 @Value("{$user.name}") pr ...

  4. SpringBoot在logback.xml中读取application.properties中配置的日志路径

    1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...

  5. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  6. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  7. Spring系列之——springboot解析resources.application.properties文件

    摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...

  8. SpringBoot配置文件 application.properties详解

    SpringBoot配置文件 application.properties详解   本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...

  9. springboot 配置 application.properties相关

    springboot 有读取外部配置文件的方法,如下优先级: 第一种是在jar包的同一目录下建一个config文件夹,然后把配置文件放到这个文件夹下.第二种是直接把配置文件放到jar包的同级目录.第三 ...

随机推荐

  1. [转帖]SAP一句话入门:Project System

    SAP一句话入门:Project System http://blog.vsharing.com/MilesForce/A621279.html 这是SAP ERP入门的最后一篇了. 我们这些死跑龙套 ...

  2. linux关闭触摸板

    关闭触摸板 sudo modprobe -r psmouse 如果打开触摸板就是: sudo modprobe psmouse

  3. Bootstrap 字体图标(Glyphicons)

    http://www.runoob.com/bootstrap/bootstrap-glyphicons.html 什么是字体图标? 字体图标是在 Web 项目中使用的图标字体.虽然,Glyphico ...

  4. springboot+ELK+logback日志分析系统demo

    之前写的有点乱,这篇整理了一下搭建了一个简单的ELK日志系统 借鉴此博客完成:https://blog.csdn.net/qq_22211217/article/details/80764568 设置 ...

  5. 谈谈对C#中反射的一些理解和认识(下)

    在上一篇中我们列举了一些反射的常规的使用,这一篇我们将介绍一些关于关于反射的高级属性,这些包括创建对反射的性能的总结以及如何优化反射性能,以及通过InvokeMember的方法如何去调用反射等等,通过 ...

  6. 死锁问题分析(个人认为重点讲到了gap间隙锁,解决了我一些不明报死锁的问题)

    线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...

  7. TensorFlow总结

    第一 基础 1. 定义变量 #定义维度为[2,3], 平均值为·1, 标准差为1,类型为float32,名称为w1的服从正态分布的变量 w1 = tf.Variable(tf.random_norma ...

  8. C# json解析

    json格式数 [{ , , , "ItemCode": "UBAC11211OF-A54", "basicName_bg": " ...

  9. java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer; at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.jav

    在整合spring和mybatis在执行数据库操作的时候报出了: java.lang.AbstractMethodError: org.mybatis.spring.transaction.Sprin ...

  10. Hadoop Brief

    Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. [1]  Hadoop实现了一个分布 ...