Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection in a Java 7 standalone application

I would like to use dependency injection in a large Java 7 standalone application, but I am not really sure where to start.

I have written a small test application:

public class Main {

    @Inject
    MyInterface myInterface;

    public static void main( String[] args ) {

        Main m = new Main();
        System.out.println(m.myInterface.getMessage());

    }

}

with an interface:

public interface MyInterface {

    String getMessage();

}

and an interface implementation:

@Singleton
public class MyInterfaceImpl implements MyInterface {

    public String getMessage() {
        return "Hello World!";
    }

}

The pom.xml contains one dependency:

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

This application compiles, but of course, it crashes with a NPE when trying to print the message. The injection has not happened.

So, my question are:

  1. Can dependency injection be achieved in a Java 7 standalone application?
  2. What other dependencies do I have to include to make it work?
  3. Does anyone have a simple operational example to share (I could not find any)?
like image 394
Jérôme Verstrynge Avatar asked Dec 12 '13 15:12

Jérôme Verstrynge


People also ask

Is there dependency injection in Java?

Dependency injection enables you to turn regular Java classes into managed objects and to inject them into any other managed object. Using dependency injection, your code can declare dependencies on any managed object.

What is a CDI application?

Overview. CDI (Contexts and Dependency Injection) is a standard dependency injection framework included in Java EE 6 and higher. It allows us to manage the lifecycle of stateful components via domain-specific lifecycle contexts and inject components (services) into client objects in a type-safe way.


1 Answers

There is not only one way to use dependency injection with Java.

(1) You could for instance use the standard CDI, where the reference implementation is Weld. There is documentation about using Weld in a Java SE environment, what is probably what you mean by standalone application.

You could alternatively also use Spring Framework, which also supports the common CDI annotation (e.g. @Inject). In this case, you will typically create a ClasspathXmlApplicationContext at the program startup and let Spring manages (create/destroy) all the beans you need.

(2) You current dependencies only imports the API of Java EE. Thus I'm not surprised if you get a NullPointerException at the execution. You need to add an implementation (like Weld) or use Spring.

(3) See links above.

Also take a look at Differences between Java EE 6 CDI Implementations to get reference about other Java CDI available implementations.

like image 169
LaurentG Avatar answered Oct 22 '22 10:10

LaurentG