All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opensymphony.xwork2.inject.Container Maven / Gradle / Ivy

There is a newer version: 6.4.0
Show newest version
/**
 * Copyright (C) 2006 Google Inc.
 *
 * Licensed 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.
 */

package com.opensymphony.xwork2.inject;

import java.io.Serializable;
import java.util.Set;

/**
 * 

* Injects dependencies into constructors, methods and fields annotated with * {@link Inject}. Immutable. *

* *

When injecting a method or constructor, you can additionally annotate * its parameters with {@link Inject} and specify a dependency name. When a * parameter has no annotation, the container uses the name from the method or * constructor's {@link Inject} annotation respectively. *

* *

For example:

* *
 *  class Foo {
 *
 *    // Inject the int constant named "i".
 *    @Inject("i") int i;
 *
 *    // Inject the default implementation of Bar and the String constant
 *    // named "s".
 *    @Inject Foo(Bar bar, @Inject("s") String s) {
 *      ...
 *    }
 *
 *    // Inject the default implementation of Baz and the Bob implementation
 *    // named "foo".
 *    @Inject void initialize(Baz baz, @Inject("foo") Bob bob) {
 *      ...
 *    }
 *
 *    // Inject the default implementation of Tee.
 *    @Inject void setTee(Tee tee) {
 *      ...
 *    }
 *  }
 * 
* *

To create and inject an instance of {@code Foo}:

* *
 *  Container c = ...;
 *  Foo foo = c.inject(Foo.class);
 * 
* * @see ContainerBuilder * @author [email protected] (Bob Lee) */ public interface Container extends Serializable { /** * Default dependency name. */ String DEFAULT_NAME = "default"; /** * Injects dependencies into the fields and methods of an existing object. * * @param o object to inject */ void inject(Object o); /** * Creates and injects a new instance of type {@code implementation}. * * @param type * @param implementation of dependency * @return instance */ T inject(Class implementation); /** * Gets an instance of the given dependency which was declared in * {@link com.opensymphony.xwork2.inject.ContainerBuilder}. * * @param type * @param type of dependency * @param name of dependency * @return instance */ T getInstance(Class type, String name); /** * Convenience method. Equivalent to {@code getInstance(type, * DEFAULT_NAME)}. * * @param type * @param type of dependency * @return instance */ T getInstance(Class type); /** * Gets a set of all registered names for the given type * @param type The instance type * @return A set of registered names or empty set if no instances are registered for that type */ Set getInstanceNames(Class type); /** * Sets the scope strategy for the current thread. * * @param scopeStrategy scope strategy */ void setScopeStrategy(Scope.Strategy scopeStrategy); /** * Removes the scope strategy for the current thread. */ void removeScopeStrategy(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy