0

I am trying to understand how javax.inject gonna work, but it seems not working as expected.

package demo333;

import javax.inject.Inject;
import javax.inject.Named;

public class Demo2452 {

    @Inject private @Named("AA1") AA aa;

    public static void main(String[] args) {
        new Demo2452().m2();
    }

    private void m2() {
        System.out.println(aa.hello());
    }

}


interface AA{
    String hello();
}

@Named("AA1")
class AA1 implements AA{

    @Override
    public String hello() {
        return "helllllooooo";
    }

}

I am getting NullPointerException as below stacktrace:

Exception in thread "main" java.lang.NullPointerException at demo333.Demo2452.m2(Demo2452.java:15) at demo333.Demo2452.main(Demo2452.java:11)

Could anyone please tell me what went wrong in above code?

john
  • 925
  • 1
  • 12
  • 20

1 Answers1

1

You need to add @Component on the class .

@Component
Class Example{}
pvpkiran
  • 25,582
  • 8
  • 87
  • 134