Link-Kou / Plugin-ConfigProperty

基于Spring,可以脱离容器使用配置获取,将配置以常理的方式改为注解注入的方式。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Plugin-ConfigProperty

Plugin-ConfigProperty 能做什么?

Spring环境中@Value具备非常强大的功能。希望能在非加载容器内的类提供类似的功能

  • 基于Spring,读取Properties文件
  • 提供读取配置注入的单一功能

Java的编译时注解;继承AbstractProcessor进行代码构建 替换常量的方式的配置

  • @ConfigValue注解注入

使用环境

JAVA1.8
Maven

Maven仓库

<dependency>
  <groupId>com.github.link-kou</groupId>
  <artifactId>config-property</artifactId>
  <version>1.0.2</version>
</dependency>

使用教程

  1. @ConfigValue会实现构建,在Spring环境中也可以使用
public class TestDemo {

   //初始化获取到项目内所有properties文件,修改后重启即可生效
   @ConfigValue(value = @Value("${Globalparam.Paging.DEFAULT_ITEMS_PER_PAGE}"))
   private Integer DEFAULT_ITEMS_PER_PAGE = 10;

   //查询不到properties文件,默认使用赋值数据
   @ConfigValue(@Value("${Globalparam.Paging.DEFAULT_ITEMS_PER_PAGE_NONE}"))
   private transient Integer DEFAULT_ITEMS_PER_PAGE_NONE = 2;

   //不支持非包装类型
   @ConfigValue(@Value("${Globalparam.Paging.DEFAULT_PAGE}"))
   private transient double DEFAULT_PAGE;

   //Config 通过Spring方式获取
   @ConfigValue(value = @Value("${Globalparam.Paging.DEFAULT_ITEMS_PER_PAGE}"), defaultValue = "5")
   private transient Config<Integer> DEFAULT_ITEMS_PER_PAGE_Config;

   @Test
   public void test() {
       final Integer integer1 = DEFAULT_ITEMS_PER_PAGE;
       final Integer integer2 = DEFAULT_ITEMS_PER_PAGE_NONE;
       final double integer3 = DEFAULT_PAGE;
       final Integer integer4 = DEFAULT_ITEMS_PER_PAGE_Config.get();
       System.out.println(integer1);
       System.out.println(integer2);
       System.out.println(integer3);
       System.out.println(integer4);
   }

}
  1. 在Spring环境XML配置
    <!--配置读取-->
    <bean class="com.linkkou.configproperty.spring.ConfigMsgPropertyConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:**/JsonResultMsgCode.properties</value>
                <value>classpath*:config/properties/globalparam.properties</value>
                <value>classpath*:config/properties/RedisKeyName.properties</value>
            </list>
        </property>
        <property name="fileEncoding">
            <value>utf-8</value>
        </property>
    </bean> 
            
  1. 非Spring环境中默认读取所有properties文件
   读取项目内所有properties文件

About

基于Spring,可以脱离容器使用配置获取,将配置以常理的方式改为注解注入的方式。


Languages

Language:Java 100.0%