Preference是Eclipse平台内置支持的几种持久化(记忆先前的状态)的机制中的一种(其它的还有DialogSettings,IMemento)。本篇主要针对Preference的Scope(范围)进行总结。
Preference持久化文件的后缀为".prefs"。
The Eclipse runtime defines three so-called scopes. The scope defines how the preference data is stored and how it is changeable.
Table 1. Eclipse Preference scope(Table1来自Lars Vogel的博客)
Scope | Description |
---|---|
Instance scope | If the user runs the same program twice, the settings between the two programs may be different. |
Configuration scope | If the user runs the same program twice then the settings between the two programs are the same. |
Default scope | Default values which can not be changed. Supplied via configuration files in plug-ins and product definitions. |
INSTANCE - The instance scope can also be thought of as "workspace". That is, the preferences which are stored in this scope are stored per workspace, or per running instance of Eclipse. This scope corresponds to the default location of preferences in Eclipse 2.1. The instance area is derived from the location returned by
org.eclipse.core.runtime.Platform#getInstanceLocation()
. Preferences stored in this scope will not be available to other running instances of Eclipse。
instance scope按照工作空间或平台的运行实例存储的,每个运行实例所存储的值可能是不一样的,不同实例之间只使用自己的值,无法与其它实例共享。
存储位置位于运行实例的如下位置:{workspace_path}\.metadata\.plugins\org.eclipse.core.runtime\.settings\{qualifier}.prefs
这点,我们可以通过阅读下面几个类去分析一下,限于时间,在此不分析源码了。所涉及类如下:org.eclipse.core.internal.preferences.InstancePreferences
org.eclipse.core.internal.runtime.DataArea
org.eclipse.core.internal.preferences.EclipsePreferences
configuration scope -The configuration scope is used to store preferences on a per configuration level. Being able store preferences per configuration means that all workspaces share the same preferences. For instance, if you are an Eclipse developer and you have a single Eclipse install but you have multiple workspaces for different projects/branches that you are working on, the preferences stored in the configuration scope will be shared between these workspaces. The configuration area is derived from the location returned by
org.eclipse.core.runtime.Platform#getConfigurationLocation()
.
configuration scope 各个工作空间将共享它们。例如,如果用户具有平台的单个安装,但是运行几个不同的工作空间,则配置级别范围的首选项将在工作空间之间共享。
存储位置位于程序安装目录的如下位置:{configuration}\.settings\{qualifier}.prefs
Default scope-表示首选项的缺省值。它们不是由平台更改或存储的。但是,这些值来源于随插件的产品或主要功能部件一起存储的文件。
另外,常用的Scope还有一个是project Scope,针对特定工程的设置,存储位置位于
{project_path}\.settings\{qualifier}.prefs
这几个Scope在eclipse中均有相应的类对应,分别为:InstanceScope,ConfigurationScope,DefaultScope,ProjectScope。
在后续的博文中,将提及这些Scope的应用。