Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple framework allowing for Dependency Injection in a stand alone program?

We basically need to be able to adjust behaviour at start-up time, by providing desired classes to be produced by various factories inside our application (to avoid the hard binding of the "new" operator).

I am aware that this is provided by several large frameworks, but I was looking for something easily used by a stand-alone Java application without being gigantic.

Any suggestions?


Edit: It is my experience that frameworks tend to grow big as part of maturing (and complex too). I need this to be retrofittable to a legacy application as part of major refactoring (technical debt), so simplicity is essential of the used libraries. I do not mind having to do a bit of coding in our application, but it must be very visible what is going on. AOP has a tendency for moving stuff out of the way, and that may make the application harder to maintain.


Edit: We have now reached the point where we actually need to make a decision. The application will probably live for decades so we need to make a reversible decision with a framework that will be maintained for hopefully as long. I really like the static type check available with Guice, but not that the annotations bind explicitly to Guice instead of being external like in Spring. I also like that code appears to be more concise when using Guice as opposed to Spring. We need something that is robust and helpful. We do not need more than just DI at the moment. Is there a use case that definitive says go for one of these?


Edit 2011-07-27: The final decision was to use the JSR-330 API in code, and choose on a per-project basis if to use Spring, Guice or Weld. For stand-alone applications Guice has worked well so far as the JSR-330 implementation.

like image 246
Thorbjørn Ravn Andersen Avatar asked Aug 26 '09 09:08

Thorbjørn Ravn Andersen


People also ask

Do I need a framework for dependency injection?

Dependency injection doesn't strictly require frameworks – Code smells series.

Which framework is used for dependency injection?

. NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.

How can I make my own dependency injection?

Create an instance of client class. Scan all the services using in the client class (member variables, constructor parameters, method parameters) Scan for all services declared inside the service itself (nested dependencies), recursively. Create instance for each service returned by step 3 and step 4.

What is dependency injection for beginners?

Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID's dependency inversion and single responsibility principles.


2 Answers

You can always use Spring Framework 2.5. It is a big one, but if you planning to use only DI you can use spring-core and spring-beans modules, which are pretty small (ca. 500KB and 300KB).

There is also Google Guice 2.0 which comes with a package with only basic stuff (no AOP) and it's 430KB.

like image 200
Tomasz Błachowicz Avatar answered Nov 10 '22 07:11

Tomasz Błachowicz


Have you looked at the Google Guice framework? It's pretty lightweight and annotation-based, avoiding XML configuration files

There's also Pico- and Nano-container (from codehaus) which are quite lightweight although the last time I looked (admittedly a few years ago) the documentation was lacking.

I must say that I agree with others about what I assume is your presumption that Spring is massive and confusing. It's really a very simple IoC container and to be recommended.

like image 25
oxbow_lakes Avatar answered Nov 10 '22 06:11

oxbow_lakes