Is there a simple structure that allows dependency injection in a standalone program?

Basically, we should be able to adjust the behavior at startup by providing the required classes that will be produced by various factories within our application (to avoid tightly binding the "new" operator).

I know this is provided by several large frameworks, but I was looking for something easily used by a standalone Java application, without a giant one.

Any suggestions?


Edit: In my experience, frameworks tend to grow as part of ripening (and complex too). I need this so that it can be retrofitted into an outdated application as part of the main refactoring (technical duty), so simplicity is important for the libraries used. I am not averse to doing a bit of coding in our application, but it should be very clearly visible what is happening. AOP tends to move material to the side, and this can make the application difficult.


Edit: we have reached the point where we really need to make a decision. The application is likely to live for many decades, so we need to make a reversible decision with a framework that will be supported with hope for a long time. I really like the static type check available with Guice, but not that the annotations are explicitly bound to Guice, and not as external as in Spring. I also like that this code seems to be more concise when using Guice, not Spring. We need something reliable and useful. At the moment, we do not need more than just DI. Is there a precedent that says the final version for one of them?


Edit 2011-07-27: The final solution was to use the JSR-330 API in the code and select for each project if you use Spring, Guice or Weld. For stand-alone applications, Guice has proven itself in implementing the JSR-330.

+6
java dependency-injection
source share
10 answers

Have you looked at Google Guice ? It is quite lightweight and annotation-based, avoiding XML configuration XML

There is also Pico - and Nano-container (from the code), which are quite lightweight, although the last time I watched (though a few years ago) that the documentation was missing.

I have to say that I agree with others regarding what I assume is your presumption that Spring is massive and confusing. This is a really very simple IoC container and is recommended.

+5
source share

You can always use Spring Framework 2.5. This is great, but if you plan to use only DI, you can use spring -core and spring modules - beans, which are quite small (around 500 KB and 300 KB).

There is also Google Guice 2.0, which comes with a package with basic material (without AOP) and 430KB.

+7
source share

There is a couple that I know that you might find useful:

I found Plexus very useful in standalone applications because it has additional utility components for interacting with the CLI.

+2
source share

By “gigantic,” I’m going to suggest that you mean Spring, but this is unfair, since you can use cherries - select the Spring bit that you want to use. If all you need is an IoC container, just use the appropriate JAR files and the corresponding API bit and ignore the rest.

+1
source share

Most of the answers so far seem to relate to the size of the jar files being added.

However, I think the more important question is the impact on the project: how many lines of code need to be added / changed in order to use the framework?

Even the "big" spring infrastructure is actually very easy to use:

You basically need to:

  • xml file that describes your plants.
  • one line of code to initialize the container by loading the xml file

The best part is that spring is non-intrusive . This way you do not need to implement specific interfaces or add any specific annotations or import your classes.

At best, the only place you actually initialize the spring container is in the application, which has an actual dependency on the spring classes.

+1
source share

I would strongly suggest taking a look at Spring ME . Although it was originally a way to use Spring for Java ME applications, it is also great for standalone applications.

True, this does not give you all the bells and whistles that Spring (Full) can offer, but again, Full Spring is much more than a simple dependency injection environment.

On the plus side: it is based on a (compatible) subset of Spring configuration files, and the size of the working environment is 0%. In fact, they are not. Spring ME will take your application context and turn it into a class that has no dependencies on classes other than yours.

+1
source share

What happened to Spring ?

Nowadays it is packed very well, so you will not need to take the entire kit and body.

As an aside, I'm not a fan of annotation-based injection frameworks. This is because annotations are attached to the class, not to the instance, and later to the assumption, imho, for DI. This means that every instance of this class gets the same object (s), which seems to defeat the point.

Also think that DI doesn't even need a framework, what's wrong with your main method connecting the application?

0
source share

If you want something as simple and appropriate as possible, write a code that will do what you want to do. Presumably, this is due to the union of factories, based partly on fixed logic, and partly on runtime settings.

This has the advantage that the set of possible run-time configurations is known and therefore documentable and verifiable.

The disadvantage is that deploying an unexpected change in logic takes essentially an extra second or so of compilation time and (more importantly) cannot be crawled into production without full testing, masking it as "just a configuration change."

0
source share

About a year ago, I asked myself a question very similar to this question. Therefore, I spend several hours reading Spring and Guice documentation. After about an hour with Spring, I had the feeling that I could get a basic web application, but had no idea how to use it in a separate application. An hour later, with the Guice document, everything clicked, and I could see how I could do what I wanted to do. Now to recommend Guice? Well no. What does your team already know? If someone already knows, tell Spring leaver that knowledge and spread it. How wise with Gis or Pico.

0
source share

If you want something really easy, you can take a look at fuse , it is quite extensible, maybe I'm looking.

amuses

N

0
source share

All Articles