spring-framework环境下freemark应用异常分析

关注公众号 `N学无止界` 获取更多

场景描述:

         某业务系统采用freemark根据模板生产文件的时候发现在同一个线程内,同一个数据源“连续”生成两次文件结果不一样。

依赖环境:

         JDK1.6  spring-framework-3.0.3  freemarker-v2.3.20

异常表现:

         同一个数据源“连续”生成两次文件结果不一样。

模拟代码:

public static void main(String[] args) {

		String templateFilePath = "H:\\temp\\javaBean\\template.txt";
		String saveFilePath = "H:\\temp\\javaBean\\result.txt";

		Map<String, Object> dataMap = new HashMap<String, Object>();
	
		// 模拟数据库获取数据
		Map testTarget = new LinkedCaseInsensitiveMap();
		testTarget.put("key", "数据库数据。");
		
		// 业务逻辑
		String dealKey = "KEY";
		if(testTarget.containsKey(dealKey)){
			testTarget.put(dealKey, "新的业务逻辑值。");
		}

		dataMap.put("map", testTarget);

		// 下面根据dataMap template 生成渲染后的模板
		try {
			HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath);
			HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath+".second");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("========");
	}

template.txt 内容为


AAAA
${map.key}
BBBB

result.txt 第一次结果:


AAAA
新的业务逻辑值。
BBBB

result.txt.second 第二次结果:


AAAA
数据库数据。
BBBB

LinkedCaseInsensitiveMap 结构为 mybatis读取数据库后生成的默认Map

连续生成两次结果不一样。

HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath);
HelperFreemarker.createFile(dataMap, templateFilePath, saveFilePath+".second");

异常分析:

  1. LinkedCaseInsensitiveMap (忽略大小写的一个Map)

            put方法 、get方法:

    @Override
	public V put(String key, V value) {
		this.caseInsensitiveKeys.put(convertKey(key), key);
		return super.put(key, value);
	}

	@Override
	public boolean containsKey(Object key) {
		return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
	}

	@Override
	public V get(Object key) {
		if (key instanceof String) {
			return super.get(this.caseInsensitiveKeys.get(convertKey((String) key)));
		}
		else {
			return null;
		}
	}

     可以看到实现大小写不敏感的方法是用 

private final Map<String, String> caseInsensitiveKeys;

这个HashMap来保存大小写的对应关系。这里当put两个大小写不同key的时候,只能取出最后一个key的值。

   2. 跟踪freemark执行代码

freemark

java/freemarker/template/SimpleHash 里面get方法

public TemplateModel get(String key) throws TemplateModelException {
        Object result = map.get(key);
        // The key to use for putting -- it is the key that already exists in
        // the map (either key or charKey below). This way, we'll never put a 
        // new key in the map, avoiding spurious ConcurrentModificationException
        // from another thread iterating over the map, see bug #1939742 in 
        // SourceForge tracker.
        final Object putKey;
        if (result == null) {
            if(key.length() == 1) {
                // just check for Character key if this is a single-character string
                Character charKey = new Character(key.charAt(0));
                result = map.get(charKey);
                if (result == null && !(map.containsKey(key) || map.containsKey(charKey))) {
                    return null;
                }
                else {
                    putKey = charKey;
                }
            }
            else if(!map.containsKey(key)) {
                return null;
            }
            else {
                putKey = key;
            }
        }
        else {
            putKey = key;
        }
        if (result instanceof TemplateModel) {
            return (TemplateModel) result;
        }
        TemplateModel tm = wrap(result);
        if (!putFailed) {
            try {
                map.put(putKey, tm);
            } catch (Exception e) {
                // If it's immutable or something, we just keep going.
                putFailed = true;
            }
        }
        return tm;
    }

 上面get的过程,对 map的值 为了

        // The key to use for putting -- it is the key that already exists in
        // the map (either key or charKey below). This way, we'll never put a 
        // new key in the map, avoiding spurious ConcurrentModificationException
        // from another thread iterating over the map, see bug #1939742 in 
        // SourceForge tracker.

对值进行put,put的时候导致只能取出 小写 key的值,原来是大写的KEY

处理办法:

  1. 开发的时候统一大小写
  2. spring framework github 上最老的代码是 3.2.x的代码,发现已经修复这个bug,可以尝试升级版本,(升级有风险需谨慎),或重写该类 spring-framework/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java at 3.2.x · spring-projects/spring-framework · GitHub
  3. freemark缓存的时候要充分考虑 final 对引用下对象谨慎处理。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack_software

感谢打赏,我努力提供优质内容~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值