maven - Java CDI not kicking in -
so, first attempt @ cdi kind of went dogs. i've read ton of articles , tried variety of simple complex examples without success. here current, simple example. 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
builds fine, on attempted run error
exception in thread "main" java.lang.nullpointerexception @ com.foo.app.cdi_test.main.main(main.java:5)
which precisely when attempt invoke saystuff()
on greeting-property. why not being instantiated? tutorials claim @default excessive well. i've attempted using both custructor-injection , 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
cdi runs fine in java ee container
e.g. try webapp, jsp/jsf page inside weblogic
in java se, may need more
what easiest way have cdi , jpa in java se?
http://blog.rocketscience.io/dependency-injection-with-cdi-in-java-se/
when new printer()
responsible injecting stuff in object?
Comments
Post a Comment