0

I have an application service class that is application scoped:

@ApplicationScoped
public class ApplicationService { ... }

I want to control which methods of the application service are exposed to the servlets, so there is a facade class. The interface is this:

@Local
public interface ApplicationLocal { ... }

The implementation is the facade with the injected real application service:

@Stateless
public class ApplicationFacade implements ApplicationLocal {

    @Inject
    private ApplicationService service;

}

The servlet class then gets the injected bean interface:

@ManagedBean
@WebServlet("/ServiceManager")
public class ServiceManagerServlet extends HttpServlet {

    @Inject
    private ApplicationLocal app;

}

When I do this, app is always null in the doPost and doGet methods. I get no errors from Wildfly on deployment or invocation of the servlet methods.

As always, all help is greatly appreciated.

SteveB
  • 483
  • 1
  • 4
  • 18
  • 1
    Have you tried annotating with `@EJB` instead of `@Inject`? – Robby Cornelissen Nov 16 '17 at 04:57
  • Yes. The app object is still null. – SteveB Nov 16 '17 at 05:04
  • 1
    In addition, add a `@Local(ApplicationLocal.class)` annotation to your `@Stateless` bean. – Robby Cornelissen Nov 16 '17 at 05:07
  • Check that. After doing a clean install, the app object is no longer null, but the application service referenced by the facade class no longer seems to be application scoped. Its state changes on every servlet invocation. – SteveB Nov 16 '17 at 05:10
  • Check that again. After doing yet another clean install, everything is working. However, I'm not sure I understand why @EJB injection would work and Weld would not. – SteveB Nov 16 '17 at 05:16
  • 1
    As far as I understand, because EJBs are instantiated by the EJB subsystem and not the CDI bean manager. – Robby Cornelissen Nov 16 '17 at 05:32
  • If you move this to a reply, I'll give you credit for the answer. – SteveB Nov 16 '17 at 12:01
  • CDI/Weld should work here just as well. The `@Stateless` bean will change state on each request, however the underlying app scoped bean should not - you seem to have some weird build problems according to comments, try running that from console and not IDE. Also the servlet class should not be `@ManagedBean` (not sure if this can cause any trouble?) - from CDI standpoint, servlet classes are (by default) non-contextual instances. This mean that you can inject into them (they are injection targets) but you cannot inject the servlet class elsewhere (they are not beans). – Siliarus Nov 20 '17 at 11:45
  • Removing the @ManagedBean annotation didn't change anything. From what I can discover, you can't inject a contextual bean into a servlet. Everything works fine as long as I have an EJB proxy class (i.e. the servlet gets the EJB injection and the proxy bean gets the CDI injection). I do all of my builds with Maven on the command line, so that doesn't seem to be a factor. – SteveB Nov 21 '17 at 17:24

0 Answers0