Mar*_*cak 5 freemarker hotswap dcevm
我正在使用Freemarker和DCEVM + HotSwapManager代理.这基本上允许我即使在添加/删除方法时也可以热交换类.
一切都像魅力一样,直到Freemarker使用hotswapped类作为模型.它正在抛出freemarker.ext.beans.InvalidPropertyException:即使反射显示该方法存在(在调试会话期间检查),我也没有这样的bean属性.
我在用
final Method clearInfoMethod = beanWrapper.getClass().getDeclaredMethod("removeIntrospectionInfo", Class.class);
clearInfoMethod.setAccessible(true);
clearInfoMethod.invoke(clazz);
Run Code Online (Sandbox Code Playgroud)
清除缓存,但它不起作用.我甚至尝试获取classCache成员字段并使用反射清除它,但它也不起作用.
我究竟做错了什么?我只需要强制freemarker抛弃他已经获得的模型类/类的任何内省.
有什么办法吗?
UPDATE
Application.java
// Application.java
public class Application
{
public static final String TEMPLATE_PATH = "TemplatePath";
public static final String DEFAULT_TEMPLATE_PATH = "./";
private static Application INSTANCE;
private Configuration freemarkerConfiguration;
private BeansWrapper beanWrapper;
public static void main(String[] args)
{
final Application application = new Application();
INSTANCE = application;
try
{
application.run(args);
}
catch (InterruptedException e)
{
System.out.println("Exiting");
}
catch (IOException e)
{
System.out.println("IO Error");
e.printStackTrace();
}
}
public Configuration getFreemarkerConfiguration()
{
return freemarkerConfiguration;
}
public static Application getInstance()
{
return INSTANCE;
}
private void run(String[] args) throws InterruptedException, IOException
{
final String templatePath = System.getProperty(TEMPLATE_PATH) != null
? System.getProperty(TEMPLATE_PATH)
: DEFAULT_TEMPLATE_PATH;
final Configuration configuration = new Configuration();
freemarkerConfiguration = configuration;
beanWrapper = new BeansWrapper();
beanWrapper.setUseCache(false);
configuration.setObjectWrapper(beanWrapper);
try
{
final File templateDir = new File(templatePath);
configuration.setTemplateLoader(new FileTemplateLoader(templateDir));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
final RunnerImpl runner = new RunnerImpl();
try
{
runner.run(args);
}
catch (RuntimeException e)
{
e.printStackTrace();
}
}
public BeansWrapper getBeanWrapper()
{
return beanWrapper;
}
}
Run Code Online (Sandbox Code Playgroud)
RunnerImpl.java
// RunnerImpl.java
public class RunnerImpl implements Runner
{
@Override
public void run(String[] args) throws InterruptedException
{
long counter = 0;
while(true)
{
++counter;
System.out.printf("Run %d\n", counter);
// Application.getInstance().getFreemarkerConfiguration().setObjectWrapper(new BeansWrapper());
Application.getInstance().getBeanWrapper().clearClassIntrospecitonCache();
final Worker worker = new Worker();
worker.doWork();
Thread.sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
Worker.java
// Worker.java
public class Worker
{
void doWork()
{
final Application application = Application.getInstance();
final Configuration freemarkerConfiguration = application.getFreemarkerConfiguration();
try
{
final Template template = freemarkerConfiguration.getTemplate("test.ftl");
final Model model = new Model();
final PrintWriter printWriter = new PrintWriter(System.out);
printObjectInto(model);
System.out.println("-----TEMPLATE MACRO PROCESSING-----");
template.process(model, printWriter);
System.out.println();
System.out.println("-----END OF PROCESSING------");
System.out.println();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (TemplateException e)
{
e.printStackTrace();
}
}
private void printObjectInto(Object o)
{
final Class<?> aClass = o.getClass();
final Method[] methods = aClass.getDeclaredMethods();
for (final Method method : methods)
{
System.out.println(String.format("Method name: %s, public: %s", method.getName(), Modifier.isPublic(method.getModifiers())));
}
}
}
Run Code Online (Sandbox Code Playgroud)
Model.java
// Model.java
public class Model
{
public String getMessage()
{
return "Hello";
}
public String getAnotherMessage()
{
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
这个例子根本不起作用.即使在运行时更改BeansWrapper也不会产生任何影响.
BeansWrapper(和DefaultObjectWrapper等)内省缓存依赖于java.beans.Introspector.getBeanInfo(aClass),而不是反射。(这是因为它将对象视为 JavaBean。)java.beans.Introspector有自己的内部缓存,因此它可以返回过时的信息,在这种情况下,它BeansWrapper只会根据该过时的信息重新创建自己的类自省数据。至于 的java.beans.Introspector缓存,它实际上是正确的,因为它建立在 Java 中的类是不可变的假设之上。如果有什么东西违反了这个基本规则,它应该确保java.beans.Introspector它的缓存被清除(以及许多其他缓存......),否则不仅仅是 FreeMarker 会被破坏。例如,在 JRebel,他们付出了很多努力来清除所有类型的缓存。我猜 DCEVM 没有这方面的资源。那么看来你得给Introspector.flushCaches()自己打电话了。
更新:有一段时间(Java 7,也许是 6)java.beans.Introspector每个线程组有一个缓存,因此您可以flushCaches()从所有线程组进行调用。这一切实际上都是实现细节,原则上可以随时更改。遗憾的是,JavaDocIntrospector.flushCaches()没有警告您......
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |