レジストリをバックエンドとして使用せずに、Windowsでjava.util.Preferencesを使用する方法はありますか?

StackOverflow https://stackoverflow.com/questions/208231

質問

java.util.Preferences APIを使用したいのですが、プログラムがWindowsレジストリの読み取りまたは書き込みを試行したくないのです。これについてどうすればいいですか?

役に立ちましたか?

解決

Javaを使用してWindowsレジストリを読み書きするそして、 java.util.Preferences API

を使用するときに、レジストリとは別のバックエンドが必要になります

Preference API を拡張できます。 Bernhard または Croft は、この記事

  

Preferences API はバックエンドニュートラルです。データがファイル、データベーステーブル、またはWindowsレジストリなどのプラットフォーム固有のストレージに保存されているかどうかを気にする必要はありません。

新しい Preferences はここにあります

別のAPIを使用するよりも、IMOの方が優れています。


たとえば、を拡張するクラスの検索 java.util.prefs.AbstractPreferences

  • XMLファイルを基にした設定ストアを使用できます:

import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.prefs.AbstractPreferences; import java.util.prefs.BackingStoreException; /** * Own implementation of the Java preferences API, that does not use * a "OS backing store" but relies on importing and exporting the * preferences via xml files. * Also, If a preference is got, but was not in the tree, it is entered. */ public class MyPreferences extends AbstractPreferences { private Map<String, String> prefs = new HashMap<String, String>(); private Map<String, AbstractPreferences> children = new HashMap<String, AbstractPreferences>(); public MyPreferences(MyPreferences parent, String name) { super(parent, name); } /** * @see java.util.prefs.AbstractPreferences#putSpi(java.lang.String, java.lang.String) */ protected void putSpi(String key, String value) { prefs.put(key, value); }


  • または、これらの設定をLDAPに保存できます:

de.tarent.ldap.prefs.LDAPSystemPreferences

import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import de.tarent.ldap.LDAPException;
import de.tarent.ldap.LDAPManager;

/**
 * @author kirchner
 * 
 * Preferences im LDAP
 */
public class LDAPSystemPreferences extends AbstractPreferences {
    LDAPManager     ldm         = null;
    Properties      properties  = new Properties();
    //Map für key/value der Preferences
    Map             cache       = new HashMap();
    //Map für timestamp der Preferences
    Map             timestamp   = new HashMap();
    private Boolean deleted     = Boolean.FALSE;

  • または、単純なプロパティファイルを使用できます。

com.adito.boot.PropertyPreferences

import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * A simple implementation for the preferences API. That stores preferences
 * in propery files. We do not have to worry about sharing the preferencese 
 * with other JVM instance so there is no need for any kind of synchronising
 * or locking.
 */
public class PropertyPreferences extends AbstractPreferences {

他のヒント

java.util.prefs.AbstractPreferencesを拡張することは常に可能です。

別の方法として、Apache Commonsの構成パッケージを使用すると、読み取りと書き込みが可能です。さまざまなソースから/への構成データ。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top