Giter Club home page Giter Club logo

inject's Introduction

inject

Simple java dependency injection framework based on the ServiceLoader API.

Sample

First we define a bean of name SingletonServiceImpl.

@Ordered(1)
@Singleton
public class SingletonServiceImpl implements SingletonService {

	@Inject
	private PrototypeBean prototype;

	public SingletonServiceImpl() {
		System.out.println(this.getClass().getName() + " instantiated");
	}

	@Override
	public PrototypeBean getInjectedBean() {
		return prototype;
	}
}

Later on we access it directly using the name, or by providing the interface:

final var singleton1 = Context.instance().getBean("SingletonServiceImpl", SingletonService.class);
final var singleton2 = Context.instance().getBean(SingletonService.class);

assertEquals(singleton1, singleton2);
assertNotNull(singleton1.getInjectedBean());

Although we didn't implement the singleton pattern in our SingletonServiceImpl, we will always get the same instance!

In case there are multiple bean implementing the same interface, the @Ordered annotation is used to determine the right bean - the lower the value the higher the load priority.

Dependency injection

All fields annotated with @Injected will be injected on bean-creation. There are several ways how this is done:

  • Compile-time-weaving using the io.spotnext.inject.instrumentation.InjectionTransformer with the maven mojo io.spot-next:spot-maven-plugin
  • Load-time-weaving using DynamicInstrumentationLoader.initialize(InjectionTransformer.class); from the library io.spot-next:spot-instrumentation
  • No weaving. If the beans dependencies have not yet injected by the class transformer the dependencies will be injected after the bean instantiation via reflection. This is perfectly fine although it has the downside that the fields are null in the constructor call. The very same mechanism (Context.getInstance).injectBeans(bean) can be used on manually created objects

inject's People

Contributors

mojo2012 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.