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