-
Notifications
You must be signed in to change notification settings - Fork 1
DevFaqLookupDefault
The default lookup is Lookup.getDefault()
.
It is the registry for global singletons and instances of objects which have been registered in the system by modules.
(In JDK 6, ServiceLoader
operates on the same principle.)
The default lookup searches in two places:
-
The META-INF/services/ Lookup contains all objects registered by modules via the Java Extension Mechanism - putting files in the
META-INF/services/
directory of their module JARs (typically done using the [http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/ServiceProvider.html@ServiceProvider
] annotation) -
The contents of the
Services/
folder of the System (configuration) Filesystem (this is harder and somewhat deprecated)
Objects contained in the default lookup are instantiated lazily when first requested. Objects returned by the default lookup may (or may not) be garbage collected if they become unreferenced.
Here is the usual usage pattern:
-
A central "controller" module defines some interface, e.g.
package controller.pkg;
public interface MyService {
void doSomething();
}
-
Each module which wants to implement that service depends on the controller module which defines the interface, and creates and registers an implementation:
@ServiceProvider(service=MyService.class)
public class MyImpl implements MyService {
public void doSomething() {....}
}
It is also possible to declaratively mask other people’s implementations and declaratively order implementations so some will take precedence.
-
The controller finds all implementations and uses them somehow:
for (MyService s : Lookup.getDefault().lookupAll(MyService.class)) {
s.doSomething();
}
<hr/>
Applies to: NetBeans 6.7 and later
The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.
This page was exported from http://wiki.netbeans.org/DevFaqLookupDefault , that was last modified by NetBeans user Jtulach on 2010-07-24T20:14:14Z.
NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.
Apache NetBeans is an effort undergoing incubation at The Apache Software Foundation (ASF).
Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects.
While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.
This wiki is an experiment pending Apache NetBeans Community approval.