0

Can I use @Named and @Inject in Java SE application? Something like this:

import javax.inject.Inject;
public class ThreadA implements Runnable {

    @Inject
    private MySignal sharedSignal;

    @Override
    public void run() {

        while (!sharedSignal.hasDataToProcess()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Finished");

    };

}

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named("sharedSignal")
@ApplicationScoped
public class MySignal {

    protected boolean hasDataToProcess = false;

    public synchronized boolean hasDataToProcess() {
        return this.hasDataToProcess;
    }

    public synchronized void setHasDataToProcess(boolean hasData) {
        this.hasDataToProcess = hasData;
    }

}

public class Main {
    public static void main(String[] args) {
        Thread a = new Thread(new ThreadA());
        a.start();
    }
}

I get this error:

Exception in thread "Thread-0" java.lang.NullPointerException at ThreadA.run(ThreadA.java:11) at java.lang.Thread.run(Unknown Source)

rozerro
  • 5,787
  • 9
  • 46
  • 94
  • Yes, of course - but there is nothing to actually _do the injection_ at runtime. You need a DI framework, something like Spring (Boot), to process these annotations and act on them. A CDI framework like [Weld](http://weld.cdi-spec.org/) would work too. In short, you cannot make use of these annotations with vanilla Java SE - you need some EE features. – Boris the Spider Apr 17 '16 at 06:58
  • @BoristheSpider The project has the libraries from JBossAS 7 which contains those EE features i.e. a CDI framework. – rozerro Apr 17 '16 at 06:59
  • 1
    Your question is quite broad and already answered in the documentation: https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_java_se – BalusC Apr 17 '16 at 09:52
  • @Yvette, seriously? I already had my doubts when leaving in the [java] tag, but this goes overboard. – BalusC Apr 17 '16 at 10:44
  • @Yvette, I tried to explicitly point out this question is too broad (basically, OP needs a "How to use CDI in Java SE" tutorial) and I pointed out the correct link the OP needs. The correct action would be to vote for close as "Too broad". The NPE is just a consequence of misconfiguration in OP's side. OP isn't anywhere asking what a NPE is. The question in its current form didn't leave any impression that OP actually did anything as to configuration; simply moving around JARs is insufficient to achieve the requirement. – BalusC Apr 17 '16 at 11:54
  • The question is absolutely not related to NPE so i's strange that many people voted for it. It's about injection, DI. That's why it's not a duplicate. Thks. – rozerro Apr 17 '16 at 15:38
  • Next time in [java], don't tell you got a NPE, but tell that variable X remains null, showing off you clearly understand NPE, so no one needs to tell you that NPE is obvious consquence of accessing a variable which is null. In spite of the wrong closure reason, your question would ultimately still be closed as "Too broad" because you just needed a whole tutorial/documentation as answer. – BalusC Apr 18 '16 at 08:46

0 Answers0