本文介绍了在Wicket中观看更新的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我们需要实现一种方法,使短信发送者可以通过上传属性文件来管理检票口信息/国际化.

In my current project we need to implement a way for texters to manage the wicket messages/internationalization via upload of property files.

也看到以下问题:管理国际化的检票口应用程序

如那里的建议,我实现了一个自定义IStringResourceLoader并将其添加到StringResourceLoader列表的开头,以覆盖已经存在的所有属性:

As suggested there, I've implemented a custom IStringResourceLoader and added it at the beginning of the StringResourceLoader list to override any properties already in place:

getResourceSettings().getStringResourceLoaders().add(0, new CustomStringResourceLoader());

但这还不够,因为可能发生更新,并且需要在运行时加载. StringResources由检票口缓存,并且仅在触发ResourceWatcher时更新.

This however is not enough, because updates can happen and need to be loaded at runtime. StringResources are cached by wicket and updated only when the ResourceWatcher is triggered.

我发现Wicket在哪里将字符串资源添加到观察者:设置中的PropertiesFactory.向观察者添加资源的方法是addToWatcher(...).但是,此方法受到保护,整个设置也表明该方法仅用于开发目的,而不用于生产.

I found where Wicket adds the string resources to the watcher: the PropertiesFactory in the settings. The method to add a resource to the watcher is addToWatcher(...). However this method is protected and also the whole setup suggests this is used for development purposes and not for production.

我设法通过扩展PropertiesFactory并有效地创建一个自定义版本来添加到设置中来使用此方法:

I managed to use this method by extending PropertiesFactory and effectively creating a custom version to add to settings:

getResourceSettings().setPropertiesFactory(new CustomPropertiesFactory(getResourceSettings()));
getResourceSettings().setResourcePollFrequency(Duration.seconds(1));

所以我的问题是:我觉得这是一个circuit回的解决方案.还有另一种方法来监视更改属性文件吗?

So my Question is: I feel this is quite the circuitious solution. Is there another way to watch for changing properties files?

推荐答案

我对问题的解决方案:

getResourceSettings().getStringResourceLoaders().add(0, new CustomResourceLoader());
getResourceSettings().getResourceFinders().add(new Path("/pathToResources"));
getResourceSettings().setResourcePollFrequency(Duration.seconds(1));

这会将我的CustomResourceLoader插入列表的开头,因此首先在此处检查所有属性.

This inserts my CustomResourceLoader at the beginning of the list so all properties are first checked there.

添加的Path告诉PropertiesFactory在检票口之外的给定任意目录中查找资源.

The added Path tells the PropertiesFactory to look for resources in a given arbitrary directory outside of wicket.

我也需要为资源文件添加自定义名称,我在CustomResourceLoader中意识到了这一点:

I needed custom names for my resource files as well, I realized this in the CustomResourceLoader:

public String loadStringResource(final Class<?> clazz, final String key, final Locale locale, final String style, final String variation) {
    final String myResourceFilename = createCustomResourceFileName(locale);
    final IPropertiesFactory pF = Application.get().getResourceSettings().getPropertiesFactory();
    final org.apache.wicket.resource.Properties props = pF.load(clazz, myResourceFilename);
    ...
}

使用PropertiesFactory加载文件时,它会自动将它们添加到内部IModificationWatcher中.

When using the PropertiesFactory to load the files, it adds them to the internal IModificationWatcher automatically.

事实证明,部分问题是资源文件采用非标准编码.可以通过在设置中的PropertiesFactory上添加特殊的IPropertyLoader来解决此问题:

It turns out that part of the problem was, that the resource files are in a non-standard encoding. This can be fixed by adding a special IPropertyLoader to the PropertiesFactory in the settings:

((PropertiesFactory) getResourceSettings().getPropertiesFactory()).getPropertiesLoaders().add(0,
            new UtfPropertiesFilePropertiesLoader("properties", "your-favorite-encoding"));

这篇关于在Wicket中观看更新的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:18