// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements.  See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
= Resources Injection

== Overview

Ignite supports the dependency injection of pre-defined Ignite resources, and supports field-based as well as method-based
injection. Resources with proper annotations will be injected into the corresponding task, job, closure, or SPI before it is initialized.

== Field-Based and Method-Based Injection

You can inject resources by annotating either a field or a method. When you annotate a field, Ignite simply sets the
value of the field at injection time (disregarding an access modifier of the field). If you annotate a method with
resource annotation, it should accept an input parameter of the type corresponding to an injected resource. If it does,
then the method is invoked at injection time with the appropriate resource passed as an input argument.

[tabs]
--
tab:Field-Based Approach[]
[source,java]
----
Ignite ignite = Ignition.ignite();

Collection<String> res = ignite.compute().broadcast(new IgniteCallable<String>() {
  // Inject Ignite instance.
  @IgniteInstanceResource
  private Ignite ignite;

  @Override
  public String call() throws Exception {
    IgniteCache<Object, Object> cache = ignite.getOrCreateCache(CACHE_NAME);

    // Do some stuff with cache.
     ...
  }
});
----
tab:Method-Based Approach[]
[source,java]
----
public class MyClusterJob implements ComputeJob {
    ...
    private Ignite ignite;
    ...
    // Inject Ignite instance.
    @IgniteInstanceResource
    public void setIgnite(Ignite ignite) {
        this.ignite = ignite;
    }
    ...
}
----
--

== Pre-defined Resources

There are a number of pre-defined Ignite resources that you can inject:

[cols="1,3",opts="header"]
|===
| Resource | Description

| `CacheNameResource` | Injects grid cache name provided via `CacheConfiguration.getName()`.
| `CacheStoreSessionResource` | Injects the current `CacheStoreSession` instance.
| `IgniteInstanceResource` | Injects the Ignite node instance.
| `JobContextResource` | Injects an instance of `ComputeJobContext`. The job context holds useful information about a
particular job execution. For example, you can get the name of the cache containing the entry for which a job was co-located.
| `LoadBalancerResource` | Injects an instance of ComputeLoadBalancer that can be used by a task to do the load balancing.
| `ServiceResource` | Injects an Ignite service by specified service name.
| `SpringApplicationContextResource` | Injects Spring's `ApplicationContext` resource.
| `SpringResource` | Injects resource from Spring's `ApplicationContext`. Use it whenever you would like to access a bean
specified in Spring's application context XML configuration.
| `TaskContinuousMapperResource` | Injects an instance of `ComputeTaskContinuousMapper`. Continuous mapping allows to
emit jobs from the task at any point, even after initial map phase.
| `TaskSessionResource` | Injects instance of `ComputeTaskSession` resource which defines a distributed session for a particular task execution.
|===
