Dims
Dims

Reputation: 50989

What is the simplest way to provide injection for use with @Inject annotation?

Recently I found that there are @Inject annotation somewhere. It can injects classes.

How can I use it from provider-side?

Suppose I want to be able to write

class MyClass1 {

    @Inject
    MyType2 variable;

}

How to make it work? What is absolutely required?

What if MyType2 is an interface of abstract class?

Upvotes: 0

Views: 67

Answers (1)

Yser
Yser

Reputation: 2106

The @Inject annotation is specified in JSR 330: Dependency Injection for Java (Link: JSR330)

Its used in the Web-Container of Web-Applications on various Application Server.

The Dependency Injection provides a Proxy class for your variable until it is really used. then the Container will inject the related class and you can use it. So you will need an container because its his task to inject the class into the injection-point.

If you want to use Dependency Injection in a Java SE Environment you can check out Google Guice (Link: Google Guice)

"What if MyType2 is an interface of abstract class?"

Its simply not possible to instantiate an abstract class, so i guess it would fail.

Upvotes: 1

Related Questions