Java反射+自定义注解实现配置文件数组加载(实现@ConfigurationProperties("xxx"))

Java、Rust 技术交流群: 783303214

一、背景

     最近有小朋友问我,怎么样在.properties文件中配置数组参数,我给他举了上篇文章中的注解@ConfigurationProperties("xxx"),但是遗憾的是他们的项目并没有接入spring,而是用netty写的什么sdk吧,我猜,所以上述注解无法使用,加上自己很久没有玩反射了,就将就着写了一个demo,以供初学者借鉴,话不多说,不懂的看注释,写的还是比较详细。

 

二、代码实现

  首先,我们定义一个注解类,并规定好其作用域等信息

  

package com.github;

import java.lang.annotation.*;

/**
 * @Author: BBSee
 * @E-mail: rolltion.zhang@foxmail.com
 * @CreateDate: 16:16 2019/7/22 0022
 * @Description:
 */
@Target({ ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BBSee {

    String value() default "";
    /**
     * The prefix of your properties fields
     */

    String prefix() default "";

    /**
     * These two boolean fields have not been analysed at present
     * if you do have this requirement,add it to the {@link com.github.PropertiesLoader<>}
     * @return
     */
    boolean ignoreInvalidFields() default false;

    boolean ignoreUnknownFields() default true;
}

  我们规定该注解只能使用于类上,即我们的pojo类上,prefix为配置文件中的字段前缀,接下来,我们定义注解解释器并对配置文件进行获取,由于时间不是很多,所以只写了核心的功能,即注解加载数组信息,根据pojo类的字段名称加载配置信息等,会有些bug,但是都无关痛痒,具体的看代码注释:

package com.github;

import com.github.conf.DataSourceNumberProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @Author: BBSee
 * @E-mail: rolltion.zhang@foxmail.com
 * @CreateDate: 16:48 2019/7/22 0030
 * @Description:
 */
@Slf4j
public class PropertiesLoader<E>  {
    private static final String DEFAULT_PROPERTIES_PATH="/application.properties";
    private  Properties properties;
    private static final String LEFT_BRACKET="[";
    private static final String RIGHT_BRACKET="]";
    /**
     * this demo uses  regular expression to match fields and array bracket,
     * it is highly recommend to use expression parsing engine,such as: ANTLR
     */
    public  static final Pattern BRACKET_PATTERN=Pattern.compile("^[a-zA-Z][a-zA-Z\\.0-9]+\\[+[0-9]+\\]$");


    public E getProperty(Class<?> clazz) throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
        /**
         * when the pojo is annotated
         **/
        if(clazz.isAnnotationPresent(BBSee.class)){
          return   getAnnotatedProperty(clazz);
        }else {
            /**when the pojo is not annotated,return an empty pojo */
            log.warn("pojo class without  annotation {@link com.github.BBSee.class} has " +
                    "not been supported yet,no data injected into class: "+clazz.getName());
            return (E)clazz.newInstance();
        }

    }

    /**
     * @param clazz
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    private E getAnnotatedProperty(Class<?> clazz) throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
        String annoPrefix=clazz.getDeclaredAnnotation(BBSee.class).prefix();
        /**
         * when a class has been annotated with the @BBSee annotation and used
         * the default prefix:empty string value,
         * this method tries to find out the .properties fields,
         * matching with a lower case of the pojo class name
         */
        String prefix=annoPrefix.isEmpty()?clazz.getName().toLowerCase():annoPrefix;
        Field[] fields=clazz.getDeclaredFields();
        Assert.hasText(prefix, "prefix name must not be null !");
        Assert.notNull(fields, "pojo fields must not be null!");
        /**
         * If you do not have Assert,using objects static methods instead
         */
        //Objects.requireNonNull(prefix,"prefix name must not be null !");
        Object pojo=clazz.newInstance();
        List<String> keys=this.getPropertyListByPrefix(prefix,null);
        /**
         * no .properties fields matched,
         * do nothing but return a empty pojo instance
         */
        if(keys==null||keys.isEmpty()){
            return (E)pojo;
        }

        for(Field field:fields){
            /**
             * only support class which is an
             *  instance of {@link java.util.List<T>}
             */

            if(java.util.List.class.isAssignableFrom(field.getType())) {
                String fieldName = field.getName();
                Type genericType = field.getGenericType();
                List data=new LinkedList();
                List<String> sortedKeys=this.getPropertyListByPrefix(prefix,fieldName);
                if (Optional.ofNullable(sortedKeys).isPresent()){
                    sortedKeys.forEach(key->{
                        data.add(properties.get(key));
                    });
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method writeMethod = pd.getWriteMethod();
                writeMethod.invoke(pojo,data);
            }
            /**do nothing with other data constructs  **/
        }
        return (E)pojo;
    }

    /**
     * find out the matching fields names by prefix
     * @param prefix
     * @return
     */
   private List<String> getPropertyListByPrefix(String prefix,String fieldName){
       List<String> keys=new LinkedList();
       Iterator iterator=properties.keySet().iterator();
       while(iterator.hasNext()){
           String keyStr=iterator.next().toString();
           /**
            * Confirm that there are at least one matching value
            */
           if(fieldName==null){
               keys.add(keyStr);
               break;
           }
           /**
            * It seems that,
            * this comparison algorithm has some security and mapping bugs,
            * but I don`t have enough time to fix it,
            * thus,fixing it by yourself
            */
           //FIXME
           if(keyStr.startsWith(prefix)&&
                   keyStr.substring(0,prefix.length()).equalsIgnoreCase(prefix)){
               if(BRACKET_PATTERN.matcher(keyStr).matches()&&
                       keyStr.substring(keyStr.indexOf(LEFT_BRACKET)-fieldName.length(),
                               keyStr.indexOf(LEFT_BRACKET)).equals(fieldName)){
                   keys.add(keyStr);
               }
               }
           }
       /**
        * SORT the keys by index before return
        */
       if (Optional.ofNullable(keys).isPresent()){
         Collections.sort(keys, (o1, o2) -> {
            int index1=getIndex(o1);
            int index2=getIndex(o2);
            if(index1>index2){
                return 1;
            }else if (index1<index2){
                return -1;
            }else {
                return 0;
            }

         });
       }
       return keys;
   }


    /**
     *
     * @param keyStr
     * @exception RuntimeException  index number
     * @return
     */
    public Integer getIndex(String keyStr){
        String indexStr=keyStr.substring(keyStr.indexOf(LEFT_BRACKET)+1,keyStr.indexOf(RIGHT_BRACKET));
        try {
            Integer index=Integer.parseInt(indexStr);
            if(index<0){
             throw new IndexOutOfBoundsException("Parse exception occurred while parsing .properties fields:"+keyStr);
            }
            return index;
        }catch (NumberFormatException e){
            throw new RuntimeException("Parse exception occurred while parsing .properties fields:"+keyStr);
        }

    }
   public void load(String...path ) throws IOException {
       properties = new Properties();
       if(path.length>0){
          for(String uri:path){
                try {
                    load(uri);
                }catch (IOException e){
                    log.error(e.getMessage());
                    continue;
                }
          }
       }else{
           load(DEFAULT_PROPERTIES_PATH);
       }
   }


   private void load(String path) throws IOException {
       try(InputStream file=PropertiesLoader.class.getResourceAsStream(path)) {
           properties.load(file);
       } catch (IOException e) {
           throw new IOException("error occurred while loading property file:"+path,e.getCause());
       }
   }

   public Properties getProperties(){
       return this.properties;
   }

   /**Test using of this class
    */

   public static void main(String[] args){
       PropertiesLoader<DataSourceNumberProperties> propertiesLoader=new PropertiesLoader();
       try {
           propertiesLoader.load();
          DataSourceNumberProperties properties= propertiesLoader.getProperty(DataSourceNumberProperties.class);
        int a=1;
           /**
            * you can handle these following exceptions in properties loader instead of throwing it
            * do it yourself
            */
       } catch (IOException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       } catch (IntrospectionException e) {
           e.printStackTrace();
       } catch (InstantiationException e) {
           e.printStackTrace();
       } catch (InvocationTargetException e) {
           e.printStackTrace();
       }
   }
}

代码可以优化,比如穿插一些设计模式,增加一些静态方法等等,比如我们定义一个pojo类:

@Data
@BBSee(prefix="data")
public class  DataSourceNumberProperties {
	private List<Integer> sources;
   
}

配置文件中数组:

data.sources[0]=1
data.sources[2]=2
data.sources[3]=3

loader根据字段解析,排序,对应相应的数据类型,代码中正则不支持中文,可以加上。

以上。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值