0

So, my first attempt at CDI kind of went to the dogs. I've read a ton of articles and tried a variety of simple to complex examples without success. Here is my current, simple example. What am I doing wrong?

Maven Project:

beans.xml (located in src/main/resources/META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                           http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.1" 
       bean-discovery-mode="all">
</beans>


Printer.java

import javax.inject.Inject;

public class Printer {
    @Inject Greeting greeting;
}


Greeting.java

import javax.enterprise.inject.Default;

@Default
public class Greeting {
    public void sayStuff(){ System.out.println("Stuff"); }
}


Main.java

public class Main {
    public static void main( String[] args ) {
        new Printer().greeting.sayStuff();
    }
}


The Error
It builds fine, but on attempted run I get the error

Exception in thread "main" java.lang.NullPointerException at com.foo.app.CDI_test.Main.main(Main.java:5)

which is precisely when I attempt to invoke sayStuff() on the greeting-property. Why is it not being instantiated? Tutorials claim @Default to be excessive as well. I've attempted using both a custructor-injection and setter-injection, no cigar.

EDIT 1 - added pom.xml dependencies

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency> 
        <groupId>javax.enterprise</groupId> 
        <artifactId>cdi-api</artifactId> 
        <version>1.1</version> 
        <scope>provided</scope> 
    </dependency> 
    <dependency> 
        <groupId>org.jboss.weld.se</groupId> 
        <artifactId>weld-se</artifactId> 
        <version>2.2.4.Final</version> 
    </dependency> 
</dependencies>

EDIT 2 - version information
- Java 1.7
- Eclipse Luna 4.4.0
- IntelliJ IDEA 13.1.4

krystah
  • 3,587
  • 2
  • 25
  • 43

2 Answers2

1

There is no dependency between CDI/Weld and EE. It works fine in SE. In order to run it in SE though you need to start the container. This is typically done by:

public static void main( String[] args ) {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    Printer printer = CDI.current().select(Printer.class).get();
    printer.getGreeting().sayStuff();
}

What you cannot do is direct field access, so printer.greeting won't work, hence you need to add a getter. In addition, I use CDI.current() to access the runtime. You'll also need to add org.jboss.weld.se:weld-se to your maven dependencies. Take a look at the weld docs for reference.

John Ament
  • 11,595
  • 1
  • 36
  • 45
0

CDI runs fine in Java EE container

e.g. try a webapp, jsp/jsf page inside weblogic

In Java SE, you may need to do more

What is the easiest way to have CDI and JPA in Java SE?

http://blog.rocketscience.io/dependency-injection-with-cdi-in-java-se/

when you do new Printer() who is responsible for injecting stuff in your object?

Community
  • 1
  • 1
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
  • Weld CDI should not be dependant on it being a webapp, should it? – krystah Sep 10 '14 at 14:48
  • That shouldn't be necessary, I've read at least 5 different articles where beans are fetched without having to explicitly ask through a container! – krystah Sep 10 '14 at 14:53
  • I can give them a try, link please? – Kalpesh Soni Sep 10 '14 at 15:22
  • Found 2 in the log least: https://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial_Part1 http://docs.oracle.com/javaee/7/tutorial/doc/cdi-basic007.htm – krystah Sep 10 '14 at 15:35
  • AutomatedTellerMachine atm = beanContainer.getBeanByType(AutomatedTellerMachine.class) ?? – Kalpesh Soni Sep 10 '14 at 15:53
  • 1
    As Kalpesh is trying to tell you: CDI is not "magic", someone will have to initialize the Application. In a Java SE environment, that someone is you. – folkol Sep 10 '14 at 16:51
  • I spent between 7 and 8 hours reading tutorials and yet that was the impression I was left with. My apologies and thanks for the replies. – krystah Sep 11 '14 at 16:48