Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006 Lasse Koskela
   *
   * 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 org.laughingpanda.beaninject;
 
 
Utility for injecting dependencies to Java objects using the Reflection API.

Supported injection strategies include:

  • Setter injection by property name
  • Field injection by field name
  • Setter injection by dependency type
  • Field injection by dependency type
  • An arbitrary, custom injection strategy

Author(s):
Lasse Koskela
 
 public class Inject {

Returns a target identifier that uses an injector that injects directly to a member field regardless of the field's visibility.

Parameters:
fieldName The name of the field to inject.
 
 	public static ITargetIdentifier field(final String fieldName) {
 		return new ITargetIdentifier() {
 			public IDependencyInjector of(final Object target) {
 				return new FieldInjector(targetfieldName);
 			}
 		};
 	}
 
 			final Class<? extends Annotationannotation) {
 		return new ITargetIdentifier() {
 			public IDependencyInjector of(Object target) {
 				return new AnnotatedFieldInjector(targetannotation);
 			}
 		};
 	}
 
 	public static IStaticTargetIdentifier staticField(final String fieldName) {
 		return new IStaticTargetIdentifier() {
 			public IDependencyInjector of(final Class<?> target) {
 				return new StaticNamedFieldInjector(targetfieldName);
 			}
 		};
 	}

Returns a target identifier that uses an injector that injects through a property setter method (regardless of the method's visibility).

Parameters:
propertyName The name of the property to inject. E.g. "foo" where the respective setter method's name is "setFoo".
 
 	public static ITargetIdentifier property(final String propertyName) {
 		return new ITargetIdentifier() {
 			public IDependencyInjector of(Object target) {
 				String methodName = "set"
 						+ propertyName.substring(0, 1).toUpperCase()
 						+ propertyName.substring(1);
 				return new MethodInjector(targetmethodName);
 			}
 		};
 	}
 
 			final Class<? extends Annotationannotation) {
 		return new ITargetIdentifier() {
 			public IDependencyInjector of(Object target) {
				return new AnnotatedMethodInjector(targetannotation);
			}
		};
	}

Returns an injector implementation which uses the given dependency object's type to infer which setter/field to inject.

Parameters:
target The target object for injection.
	public static IVarargDependencyInjector bean(final Object target) {
			public void with(Object... dependencies) {
				for (Object dependency : dependencies) {
					injector.validateInjectionOf(targetdependency);
				}
				for (Object dependency : dependencies) {
					injector.inject(targetdependency);
				}
			}
		};
	}

Returns an injector implementation which delegates actual injection to the given strategy when provided with a target to inject.

Parameters:
strategy The injection strategy.
	public static ITargetInjector with(final IInjectionStrategy strategy) {
		return new ITargetInjector() {
			public void bean(Object target) {
				strategy.inject(target);
			}
		};
	}
	public static IDependencyInjector staticFieldOf(final Class<?> targetClass) {
		return new IDependencyInjector() {
			public void with(Object dependency) {
				new StaticTypedFieldInjector(targetClass).with(dependency);
			}
		};
	}
New to GrepCode? Check out our FAQ X