FreeMarker下,前端js读取Constant.class

FreeMarker下,前端js读取Constant文件,获取属性或调用方法

一、 创建一个class(PropertyConfigurer),通过继承org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,获取processProperties方法

import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

private Properties props;

protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        this.props = props;
}

二、 写一个工具类(ConstantUtil),获取单个属性,方法为getConstantValue()

public static <T> T getConstantValue(String param) throws Exception {
	// 声明一个Object作为返回值
	Object result = null;
	// 拿到上面创建的PropertyConfigurer.class,用来读取properties文件内容
	PropertyConfigurer propertyConfig = (PropertyConfigurer)SpringUtil.getBean(PropertyConfigurer.class);
	// 在properties文件中配置好需要预加载的常量路径(多个以“逗号”隔开)
	String path = propertyConfig.getProperty("system.constant.class.path");
	// 拿到多个constant类的全路径
	String[] constantClass = path.split(",");
	// 循环
	for(int i = 0; i < constantClass.length; ++i) {
		// 分割字符串,PS:param为方法入参,e.g. YesOrNo.YES
	    String[] array = StringUtils.split(param, "\\.");
	    // constant内部类名称
	    String className = array[0];
	    // 内部类的属性
	    String constant = array[1];
	    // 如果异常,就尝试下一个constant
	    try {
		    // 拼接constant路径及内部类名称
		    Class<?> clazz = Class.forName(constantClass[i] + "$" + className);
		    // 得到该内部类下的属性的值
	        Field field = clazz.getDeclaredField(constant);
	        // 允许使用反射时访问私有变量
	        field.setAccessible(true);
	        // 拿到对象实例的域成员的值
	        result = field.get(clazz);
        } catch (Exception e) {
        	// do nothing
        }
	}
	return result;
}

三、 同样在ConstantUtil中再写一个方法getComboList(),获取内部类中的属性集合

 public static String getComboList(String methodPath) throws Exception {
	// 声明一个List作为返回值
	List comboList = null;
	// 拿到上面创建的PropertyConfigurer.class,用来读取properties文件内容
	PropertyConfigurer propertyConfig = (PropertyConfigurer)SpringUtil.getBean(PropertyConfigurer.class);
	// 在properties文件中配置好需要预加载的常量路径(多个以“逗号”隔开)
	String path = propertyConfig.getProperty("system.constant.class.path");
	// 用“逗号”分割,拿到多个constant类的全路径
	String[] constantClass = path.split(",");
	// 循环
	for(int i = 0; i < constantClass.length; i++) {
		// 分割字符串,PS:methodPath为方法入参,e.g. YesOrNo.getComboList(这个是方法名哦)
		String[] param = methodPath.split("\\.");
		// constant内部类名称(拼接了包路径)
        String className = constantClass[i] + "$" + param[0];
        // 内部类的方法名
        String methodName = param[1];
        // 如果异常,就尝试下一个constant
	    try {
	        // 拿到内部类的class
	        Class clz = Class.forName(className);
	        // 实例化一下下
	        Object obj = clz.newInstance();
	        // 拿到方法的对象
	        Method m = obj.getClass().getDeclaredMethod(methodName);
	        // invoke,调用对应的方法,获得其返回值(例子里返回的是个List)
	        comboList = (List)m.invoke(obj);
        } catch (Exception e) {
        	// do nothing
        }
	}
}

四、关于SpringUtils.getBean(className)工具类

public class SpringUtil implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    public SpringUtil() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        setGrobalApplicationContext(applicationContext);
    }

    public static void setGrobalApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    public static Object getBean(String name, Class requiredType) throws BeansException {
        return applicationContext.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.isSingleton(name);
    }

    public static Class getType(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getType(name);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getAliases(name);
    }
}

五、顺带提一下如何加载properties文件
https://blog.csdn.net/weixin_44554160/article/details/86678808

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值