0

I've got a jsf-hibernate application running on a Tomcat v7.0 Server and my problem is that the service is never injected to the handler, so I get a Nullpointer at getService().testMethod1() in the TestWELDHandler.

And maybe someone has an idea why? I was trying to add everything like described here https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html

So my handler looks like

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestWELDHandler {

  private static final Logger log = LoggerFactory.getLogger(TestWELDHandler.class);

  @Inject
  private TestWELDService     service;

  public TestWELDHandler() {
    log.info("TestWELDHandler");
  }

  /**
   * The method annotated with PostConstruct MUST be invoked even if the class does not request any
   * resources to be injected.
   */
  @PostConstruct
  void init() {
    log.info("init service null " + (this.service == null));
  }

  public String testMethod1FromService() {
    log.info("testMethod1FromService service null " + (this.service == null));
    return getService().testMethod1();
  }

  public TestWELDService getService() {
    return this.service;
  }

  public void setService(TestWELDService service) {
    this.service = service;
  }

}

and I've got an interface

public interface TestWELDService {
  String testMethod1();
}

and an implementation

import javax.enterprise.inject.Default;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Default
public class TestWELDServiceImpl implements TestWELDService {

  private static final Logger log = LoggerFactory.getLogger(TestWELDServiceImpl.class);

  public TestWELDServiceImpl() {
    log.info("TestWELDServiceImpl");
  }

  @Override
  public String testMethod1() {
    log.info("TestWELDServiceImpl.testMethod1");
    return "TestWELDServiceImpl.testMethod1";
  }

}

I've added

<resource-env-ref>
   <resource-env-ref-name>BeanManager</resource-env-ref-name>
   <resource-env-ref-type>
      javax.enterprise.inject.spi.BeanManager
   </resource-env-ref-type>
</resource-env-ref>

to my /webapp/WEB-INF/web.xml

and

<Resource name="BeanManager" auth="Container"
        type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" />

to the context element in /webapp/META-INF/context.xml

What I also had to do add

-Dcom.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager=true

To the VM arguments of my Tomcat to get rid of the

avax.naming.NameNotFoundException: Name [CDIExtension] is not bound in this Context. Unable to find [CDIExtension].

the bean config used for the TestWELDHandler looks like this:

<managed-bean>
        <managed-bean-name>testWELDHandler</managed-bean-name>
        <managed-bean-class>com.prosol.faces.handler.weldtest.TestWELDHandler</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
JavaMan
  • 1,142
  • 12
  • 22
  • The instance of `TestWELDHandler` is managed by JSF, as indicated by the `` configuration (probably the NPE occurs when accessing the `TestWELDHandler` from your JSF XHTMLs). If so, then `@Inject` will not work. You have to [integrate JSF with CDI](https://docs.jboss.org/weld/reference/latest/en-US/html/ri-spi.html#_jsf) and let CDI manage all bean instances. – Nikos Paraskevopoulos Nov 07 '18 at 15:44
  • yes, that's correct, the NPE occurs when accessing the TestWeldHandler from the xhtml, is there something like a simple example somewhere in the internet (I can't find one), which is relatively equal to my use case? – JavaMan Nov 08 '18 at 09:09

0 Answers0