Preference(首选项)保存配置文件的调查

获得首选项的默认为

IPreferenceStore store = new AbstractUIPlugin().getPreferenceStore();

进入这个方法

public IPreferenceStore getPreferenceStore()
/* */ {
/* 283 */ if (this.preferenceStore == null) {
/* 284 */ this.preferenceStore = new ScopedPreferenceStore(new InstanceScope(), getBundle().getSymbolicName());
/* */ }
/* */
/* 287 */ return this.preferenceStore;
/* */ }

而ScopedPreferenceStore类又继承与AbstractScope类
看里面的方法getNode()

public IEclipsePreferences getNode(String qualifier)
/* */ {
/* 36 */ if (qualifier == null)
/* 37 */ throw new IllegalArgumentException();
/* 38 */ return ((IEclipsePreferences)PreferencesService.getDefault().getRootNode().node(getName()).node(qualifier));
/* */ }

然后看PreferencesService.getDefault()返回的PreferencesService。

public static PreferencesService getDefault()
/* */ {
/* 70 */ if (instance == null)
/* 71 */ instance = new PreferencesService();
/* 72 */ return instance;
/* */ }

追踪到PreferencesService类里的构造函数

PreferencesService()
/* */ {
/* 81 */ initializeDefaultScopes();
/* */ }

继续追踪
private void initializeDefaultScopes() {
/* 371 */ this.defaultScopes.put("default", new DefaultPreferences());
/* 372 */ root.addChild("default", null);
/* 373 */ this.defaultScopes.put("instance", new InstancePreferences());
/* 374 */ root.addChild("instance", null);
/* 375 */ this.defaultScopes.put("configuration", new ConfigurationPreferences());
/* 376 */ root.addChild("configuration", null);
/* */ }

有三个scope。
继续追踪到里面
可以看到


private static IPath getBaseLocation()
/* */ {
/* 41 */ if (baseLocation == null) {
/* 42 */ Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation();
/* 43 */ if ((instanceLocation != null) && (((instanceLocation.isSet()) || (instanceLocation.allowsDefault()))))
/* 44 */ baseLocation = MetaDataKeeper.getMetaArea().getStateLocation("org.eclipse.core.runtime");
/* */ }
/* 46 */ return baseLocation;
/* */ }

baseLocation即为存放配置文件的根目录
我的电脑上为
D:\Interstage&IIM\eclipse_3.4.1\runtime-EclipseApplication\.metadata\.plugins\org.eclipse.core.runtime
(你会发现有个以工程名为前缀,.pref为后缀的文件生成。只有关了)
当我们调用getString等方法时,会调用getNode方法。。。
IEclipsePreferences这个类,保存了配置文件的信息。
最后

public String get(String key, String defaultValue, Preferences[] nodes)
/* */ {
/* 457 */ if (nodes == null)
/* 458 */ return defaultValue;
/* 459 */ for (int i = 0; i < nodes.length; ++i) {
/* 460 */ Preferences node = nodes[i];
/* 461 */ if (node != null) {
/* 462 */ String result = node.get(key, null);
/* 463 */ if (result != null)
/* 464 */ return result;
/* */ }
/* */ }
/* 467 */ return defaultValue;

利用Preferences中保存的信息,根据key获取value

后记、
preference 机制由 org.eclipse.core.runtime 插件提供,可保持的值类型包括:boolean, byte[], long, int, String, float, double。这些值保存在如下图所示的结构中:


Eclipse 运行时环境定义了三种基本的 scope:
(1)Instance scoped:单个 workspace 或者 eclipse 实例范围
(2)Configuration scoped:eclipse configuration 范围内的多个实例共享 preference
(3)Default scoped:描述 preference 的默认值,由 plugin 和 product 初始文件进行定义,而不能被 eclipse 运行时修改和存储。

[color=red]注意:[/color]只有eclipse退出后,才会自动保存。、、、

不正常的退出不会保存、、断电或者任务管理器中的强制退出、、、

之前一直以为会在PreferenceStore初始化的时候加载配置文件,其实是在getString()的时候,调用AbstractScope的方法getNode()返回IEclipsePreferences对象,里面有配置信息,然后根据这个对象获得配置信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值