Dependency Injection not working in Wildfly-Swarm-2016.12.1

1.8k views Asked by At

I am a newbie to the wildfly-swarm. I was able to prep a basic JAX-RS application in swarm, and worked like a charm. But when I'd tried to introduce CDI to my app, everything went wrong. The Bean Injection is not working!. I'm getting NULLPointerException in places where I'd tried to invoke the bean methods. I'd tried all possible solutions, that I could find over web. But nothing worked. Following is my pom.xml config;

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.uss</groupId>
    <artifactId>hatts-swarm</artifactId>
    <version>0.0.1-M0</version>
    <packaging>war</packaging>
    <properties>
        <version.wildfly.swarm>2016.12.1</version.wildfly.swarm>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>bom-all</artifactId>
                <version>${version.wildfly.swarm}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>jax_cdi</finalName>
        <plugins>
            <plugin>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>wildfly-swarm-plugin</artifactId>
                <version>${version.wildfly.swarm}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- Java EE 7 dependency -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>       
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jaxrs</artifactId>
            <version>2016.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jaxrs-cdi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>weld</artifactId>
        </dependency>


    </dependencies>
</project>

and following is my Main class;

public class BootStrap {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {

            Swarm swarm = new Swarm(true);

            JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class, "jax_cdi_microprofile");
            deployment.addClass(RESTEntryManager.class).addClass(HTML5Generator.class)
                    .addClass(HattsACKTestService.class)
                    .addAsWebInfResource(new StringAsset(new StringBuilder("<?xml version=\"1.0\"?>")
                            .append(" <beans bean-discovery-mode=\"all\" version=\"1.1\" ")
                            .append(" xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"")
                            .append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")
                            .append(" xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd\"/>")
                            .toString()), "beans.xml")
                    .addAllDependencies();

            swarm.start().deploy(deployment);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

and following is my JAX-RS resource declaration;

@Path("/handshake-test")
@Stateless
public class HattsACKTestService {

    @Resource(lookup = "java:jboss/ee/concurrency/executor/default")
    private ManagedExecutorService managedExecutorService;

    @Inject
    private Authenticator authenticator;

    public HattsACKTestService() {
        // INTENTIONALLY LEFT AS BLANK
    }

    @GET
    public void handshake(@Suspended AsyncResponse asyncResponse) {

        /*
         * new Thread(() -> {
         * 
         * HTML5Generator html5Generator = new HTML5Generatorg.Builder()
         * .setPageTitle("HATTS Handshake Service Response").appendPageBody(
         * "H1", "Handshake Initiated...") .build();
         * 
         * asyncResponse.resume(html5Generator.getContentBuilder().toString());
         * 
         * }).start();
         */

        /*managedExecutorService.submit(() -> {
            asyncResponse.resume("Hi World!!!.. This is Wildfly in CDI !!!");
        });*/
        asyncResponse.resume(authenticator.authenticate());
    }
}

and following is the Authenticator bean;

@Stateless
public class Authenticator {

    /**
     * 
     */
    public Authenticator() {
        // TODO Auto-generated constructor stub
    }

    public String authenticate() {
        return "Authenticated !!";
    }

}

Not even the ManagedExectorService is injected!. Could any one help me to sort this out ?

Updating with the latest Error got, after trying the solution mentioned by Ken

2017-01-08 20:31:01,290 ERROR [stderr] (main) java.lang.reflect.InvocationTargetException
2017-01-08 20:31:01,291 ERROR [stderr] (main)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-01-08 20:31:01,291 ERROR [stderr] (main)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-01-08 20:31:01,292 ERROR [stderr] (main)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-01-08 20:31:01,292 ERROR [stderr] (main)   at java.lang.reflect.Method.invoke(Method.java:498)
2017-01-08 20:31:01,292 ERROR [stderr] (main)   at org.wildfly.swarm.bootstrap.MainInvoker.invoke(MainInvoker.java:37)
2017-01-08 20:31:01,293 ERROR [stderr] (main)   at org.wildfly.swarm.bootstrap.Main.run(Main.java:44)
2017-01-08 20:31:01,293 ERROR [stderr] (main)   at org.wildfly.swarm.bootstrap.Main.main(Main.java:35)
2017-01-08 20:31:01,294 ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0007: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"hatts.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"hatts.war\".WeldStartService: Failed to start service
2017-01-08 20:31:01,294 ERROR [stderr] (main)     Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Authenticator with qualifiers @Default
2017-01-08 20:31:01,294 ERROR [stderr] (main)   at injection point [UnbackedAnnotatedField] @Inject private com.uss.hatts.rest.acktest.HattsACKTestService.authenticator
2017-01-08 20:31:01,294 ERROR [stderr] (main)   at com.uss.hatts.rest.acktest.HattsACKTestService.authenticator(HattsACKTestService.java:0)
2017-01-08 20:31:01,294 ERROR [stderr] (main) "},"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"hatts.war\".WeldStartService"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
2017-01-08 20:31:01,295 ERROR [stderr] (main)   at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:280)
2017-01-08 20:31:01,295 ERROR [stderr] (main)   at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:95)
2017-01-08 20:31:01,295 ERROR [stderr] (main)   at org.wildfly.swarm.Swarm.deploy(Swarm.java:475)
2017-01-08 20:31:01,295 ERROR [stderr] (main)   at org.wildfly.swarm.Swarm.main(Swarm.java:612)
2017-01-08 20:31:01,295 ERROR [stderr] (main)   ... 7 more
2017-01-08 20:31:01,296 ERROR [stderr] (main) Exception in thread "main" java.lang.reflect.InvocationTargetException
2017-01-08 20:31:01,296 ERROR [stderr] (main)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-01-08 20:31:01,301 ERROR [stderr] (main)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-01-08 20:31:01,303 ERROR [stderr] (main)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-01-08 20:31:01,304 ERROR [stderr] (main)   at java.lang.reflect.Method.invoke(Method.java:498)
2017-01-08 20:31:01,304 ERROR [stderr] (main)   at org.wildfly.swarm.bootstrap.MainInvoker.invoke(MainInvoker.java:37)
2017-01-08 20:31:01,304 ERROR [stderr] (main)   at org.wildfly.swarm.bootstrap.Main.run(Main.java:44)
2017-01-08 20:31:01,304 ERROR [stderr] (main)   at org.wildfly.swarm.bootstrap.Main.main(Main.java:35)
2017-01-08 20:31:01,305 ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0007: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"hatts.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"hatts.war\".WeldStartService: Failed to start service
2017-01-08 20:31:01,305 ERROR [stderr] (main)     Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Authenticator with qualifiers @Default
2017-01-08 20:31:01,305 ERROR [stderr] (main)   at injection point [UnbackedAnnotatedField] @Inject private com.uss.hatts.rest.acktest.HattsACKTestService.authenticator
2017-01-08 20:31:01,305 ERROR [stderr] (main)   at com.uss.hatts.rest.acktest.HattsACKTestService.authenticator(HattsACKTestService.java:0)
2017-01-08 20:31:01,305 ERROR [stderr] (main) "},"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"hatts.war\".WeldStartService"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
2017-01-08 20:31:01,305 ERROR [stderr] (main)   at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:280)
2017-01-08 20:31:01,306 ERROR [stderr] (main)   at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:95)
2017-01-08 20:31:01,306 ERROR [stderr] (main)   at org.wildfly.swarm.Swarm.deploy(Swarm.java:475)
2017-01-08 20:31:01,306 ERROR [stderr] (main)   at org.wildfly.swarm.Swarm.main(Swarm.java:612)
2017-01-08 20:31:01,308 ERROR [stderr] (main)   ... 7 more
1

There are 1 answers

2
Ken On BEST ANSWER

Your Maven dependencies need to be:

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>jaxrs</artifactId>
    </dependency>
    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>cdi</artifactId>
    </dependency>

weld was renamed to cdi a few releases ago, and jaxrs-cdi is no longer explicitly needed. It's brought in for you.

With what you have in a custom main(), I'd recommend not using one at all and just letting WF Swarm create your deployment for you.