BeanInject

Simple utility for dependency injection.

License

License

GroupId

GroupId

com.github.lkoskela
ArtifactId

ArtifactId

beaninject
Last Version

Last Version

0.9
Release Date

Release Date

Type

Type

jar
Description

Description

BeanInject
Simple utility for dependency injection.
Project URL

Project URL

http://github.com/lkoskela/beaninject
Source Code Management

Source Code Management

https://github.com/lkoskela/beaninject

Download beaninject

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.lkoskela/beaninject/ -->
<dependency>
    <groupId>com.github.lkoskela</groupId>
    <artifactId>beaninject</artifactId>
    <version>0.9</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lkoskela/beaninject/
implementation 'com.github.lkoskela:beaninject:0.9'
// https://jarcasting.com/artifacts/com.github.lkoskela/beaninject/
implementation ("com.github.lkoskela:beaninject:0.9")
'com.github.lkoskela:beaninject:jar:0.9'
<dependency org="com.github.lkoskela" name="beaninject" rev="0.9">
  <artifact name="beaninject" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lkoskela', module='beaninject', version='0.9')
)
libraryDependencies += "com.github.lkoskela" % "beaninject" % "0.9"
[com.github.lkoskela/beaninject "0.9"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.5

Project Modules

There are no modules declared in this project.

BeanInject

BeanInject is a utility for simple injection of dependencies to "beans", that is, plain old Java objects.

It's not a dependency injection framework. It's an injection utility. You figure out what to inject and where, and use BeanInject to do the dirty work of manipulating the target object with Java's Reflection API.

How to install it?

Assuming you're using Maven, just add the following dependency block into your POM file:

<dependency>
    <groupId>beaninject</groupId>
    <artifactId>beaninject</artifactId>
    <version>0.6</version>
</dependency>

How to use it?

The core of BeanInject is a class named Inject. Below is an example of its usage.

Consider an instance of the following class being the "target" of injection:

public class Target {
    private String field;
    
    public void setPropertyName(String value) {
        this.field = value;
    }
    
    public String getPropertyName() {
        return field;
    }
}

Now, for an instance of the above Target class, we can use BeanInject to inject the value "value" to the field "field" either directly or using the appropriate setter method.

Property-based injection

This is what the API call would look like for injecting through the property named "propertyName" (i.e. using a setter method "setPropertyName"):

Inject.property("propertyName").of(target).with("value");

Field-based injection

This is what the API call would look like for injecting directly to the field:

Inject.field("field").of(target).with("value");

Type-based injection

A third, perhaps less common method of injection is what we call type-based injection. With type-based injection, BeanInject will try to figure out a property or field to which the given object (value) should be injected based on its type (class). For the above Target class with a property of type java.lang.String, we might use the type-based injection as follows:

Inject.bean(target).with("value");

For the rather common situation where a setter method stores the given object into a field of the same type, the Inject class will always invoke the setter method, thus not bypassing any processing or validation the setter might implement.

Using a custom injection strategy

If none of the built-in injection strategies do it for you, there's the option of implementing your own injection strategy and pass it to the Inject class as follows:

Inject.with(new CustomInjectionStrategy()).bean(target);

The custom strategy class needs to implement the IInjectionStrategy interface:

public interface IInjectionStrategy {
   void inject(Object target);
}

Browse the source, Luke

For a more thorough understanding of how BeanInject works, take a look at its unit tests at https://github.com/lkoskela/beaninject/tree/master/src/test/java/org/laughingpanda/beaninject

License

Copyright © 2007 original author or authors.

BeanInject is Licensed under the Apache License, Version 2.0. Please refer to the URL http://www.apache.org/licenses/LICENSE-2.0 for details.

Versions

Version
0.9