Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Eclipse4/RCP/EAS/Obtaining Services"
(New page: The services are set by the dependency injection framework when your objects gets instantiated (or explicitly injected). The services that should be injected can be marked by the <tt>@Inje...) |
(→Service retrieval through the context) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | The services are set by the dependency injection framework when your objects gets instantiated (or explicitly injected) | + | The services are set by the dependency injection framework when your objects gets instantiated (or explicitly injected). |
+ | ==Injection of objects== | ||
+ | The services that should be injected can be marked by the <tt>@Inject</tt> annotation. | ||
+ | |||
+ | ===Fields=== | ||
<source lang="java"> | <source lang="java"> | ||
public class AccountsPart { | public class AccountsPart { | ||
Line 6: | Line 10: | ||
private Logger logger; | private Logger logger; | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | ===Methods=== | ||
+ | <source lang="java"> | ||
+ | public class AccountsPart { | ||
+ | private Logger logger; | ||
+ | |||
+ | @Inject | ||
+ | void setLogger(Logger logger) { | ||
+ | this.logger = logger; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Injection for method invocation== | ||
+ | Here is an example of a handler's method being invoked by the framework with its parameters being provided through dependency injection. | ||
+ | |||
+ | <source lang="java"> | ||
+ | public class ShowPartHandler { | ||
+ | @Execute | ||
+ | void execute(EPartService partService) { | ||
+ | String partId = /* retrieve the desired id */; | ||
+ | partService.showPart(partId, EPartService.PartState.ACTIVATE); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Service retrieval through the context== | ||
+ | Services can also be retrieved from the <tt>IEclipseContext</tt> using its <tt>get(Class<T>)</tt> and <tt>get(String)</tt> methods. | ||
+ | |||
+ | ===java.lang.Class=== | ||
+ | <source lang="java"> | ||
+ | EPartService partService = context.get(EPartService.class); | ||
+ | </source> | ||
+ | |||
+ | ===java.lang.String=== | ||
+ | <source lang="java"> | ||
+ | MPart part = (MPart) context.get(IServiceConstants.ACTIVE_PART); | ||
</source> | </source> |
Latest revision as of 14:08, 14 April 2011
The services are set by the dependency injection framework when your objects gets instantiated (or explicitly injected).
Contents
Injection of objects
The services that should be injected can be marked by the @Inject annotation.
Fields
public class AccountsPart { @Inject private Logger logger; }
Methods
public class AccountsPart { private Logger logger; @Inject void setLogger(Logger logger) { this.logger = logger; } }
Injection for method invocation
Here is an example of a handler's method being invoked by the framework with its parameters being provided through dependency injection.
public class ShowPartHandler { @Execute void execute(EPartService partService) { String partId = /* retrieve the desired id */; partService.showPart(partId, EPartService.PartState.ACTIVATE); } }
Service retrieval through the context
Services can also be retrieved from the IEclipseContext using its get(Class<T>) and get(String) methods.
java.lang.Class
EPartService partService = context.get(EPartService.class);
java.lang.String
MPart part = (MPart) context.get(IServiceConstants.ACTIVE_PART);