레지스트리를 백엔드로 사용하지 않고 Windows에서 java.util.preferences를 사용하는 방법이 있습니까?

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

문제

Java.util.preferences API를 사용하고 싶지만 프로그램이 Windows 레지스트리를 읽거나 쓰려고 시도하지 않기를 바랍니다. 이걸 어떻게할까요?

도움이 되었습니까?

해결책

나는 당신이 읽었다고 믿습니다 Java를 사용하여 Windows 레지스트리를 읽고/쓰십시오 그런 다음 사용할 때 레지스트리보다 다른 백엔드를 원합니다. java.util.Preferences API

당신은 확장 할 수 있습니다 Preference API, 처럼 베른하드 또는 소작지 설명했듯이 이 기사:

때문에 선호도 API 백엔드 중립이므로 데이터가 파일, 데이터베이스 테이블 또는 Windows 레지스트리와 같은 플랫폼 별 스토리지에 저장되어 있는지 신경 쓰지 않아도됩니다.

확장의 예 새로운 Preferences 여기에서 볼 수 있습니다.

다른 API를 사용하는 것보다 IMO가 더 좋습니다.


예를 들어, 클래스 확장을 검색합니다 java.util.prefs.AbstractPreferences:

  • XML 파일로 뒷받침되는 환경 설정 저장소를 사용할 수 있습니다.

de.unika.ipd.grgen.util.MyPreferences

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