wordpress做的网站吗,中国做的最好的网站有哪些,北京网站建设产品介绍,上海阿里巴巴做网站java代码防止sql注入在本文中#xff0c;我们将研究如何将Java代码动态加载到正在运行的jvm中。 该代码可能是全新的#xff0c;或者我们可能想更改程序中某些现有代码的功能。 #xff08;在开始之前#xff0c;您可能想知道为什么到底有人会这样做。显而易见的示例是规则… java代码防止sql注入 在本文中我们将研究如何将Java代码动态加载到正在运行的jvm中。 该代码可能是全新的或者我们可能想更改程序中某些现有代码的功能。 在开始之前您可能想知道为什么到底有人会这样做。显而易见的示例是规则引擎之类的东西。规则引擎希望为用户提供添加或更改规则的能力而不必重新启动规则。您可以通过将DSL脚本作为规则注入规则引擎来执行此操作这种方法的真正问题是必须对DSL脚本进行解释使其运行起来非常缓慢。然后可以像程序中的其他任何代码一样编译和运行该程序效率将提高几个数量级。 在《纪事报》中我们在新的微秒微服务/算法容器的核心中使用了这个想法。 我们将要使用的库是Chronicle开源库Java-Runtime-Compiler 。 从下面的代码中您将看到该库的使用极其简单-实际上它实际上只需要几行。 创建一个CachedCompiler然后调用loadFromJava。 有关实际最简单的用例请参见此处的文档。 下面列出的程序执行以下操作 创建一个线程该线程每秒调用一次Strategy上的compute。 该战略的投入为10和20。 加载将两个数字加在一起的策略 等待3秒 加载从另一个数中减去一个数的策略 这是完整的代码清单 package test;import net.openhft.compiler.CachedCompiler;/*** Loads the addingStrategy and then after 3s replaces it with the * subtractingStrategy.*/
public class DynamicJavaClassLoading {private final static String className test.MyClass;private final static String addingStrategy package test;\n import test.DynamicJavaClassLoading.Strategy;\n public class MyClass implements Strategy {\n public int compute(int a, int b) {\n return ab;\n }\n }\n;private final static String subtractingStrategy package test;\n import test.DynamicJavaClassLoading.Strategy;\n public class MyClass implements Strategy {\n public int compute(int a, int b) {\n return a-b;\n }\n }\n;public static void main(String[] args) throws Exception {StrategyProxy strategy new StrategyProxy();//Thread calling the strategy once a secondThread t new Thread(() - {while (true) {System.out.println(strategy.compute(10,20));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();{ClassLoader cl new ClassLoader() {};CachedCompiler cc new CachedCompiler(null, null);Class aClass cc.loadFromJava(cl, className, addingStrategy);Strategy runner (Strategy) aClass.newInstance();strategy.setStratgey(runner);}Thread.sleep(3000);{ClassLoader cl new ClassLoader() {};CachedCompiler cc new CachedCompiler(null, null);Class aClass cc.loadFromJava(cl, className, subtractingStrategy);Strategy runner (Strategy) aClass.newInstance();strategy.setStratgey(runner);}}public interface Strategy{int compute(int a, int b);}public static class StrategyProxy implements Strategy{private volatile Strategy underlying;public void setStratgey(Strategy underlying){this.underlying underlying;}public int compute(int a, int b){Strategy underlying this.underlying;return underlying null ? Integer.MIN_VALUE : underlying.compute(a, b);}}
} 这是输出蓝色注释 The strategy has not been loaded yet. underlying in the StrategyProxy is null so Integer.MIN_VALUE is returned
-2 1 4 7 4 8 3 6 4 8
The adding strategy has been loaded 102030
30
30
30
After 3s the subtracting strategy is loaded. It replaces the adding strategy. 10-20-10
-10
-10
-10
-10-10 注意在代码中每次加载策略时我们都会创建一个新的ClassLoader和一个CachedCompiler。 这样做的原因是ClassLoader一次只能加载一个特定类的一个实例。 如果仅使用此库来加载新代码则可以这样做而无需创建ClassLoader即使用默认的ClassLoader和CachedCompiler。 Class aClass CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);翻译自: https://www.javacodegeeks.com/2015/10/dynamic-java-code-injection.htmljava代码防止sql注入