1

I am building a JSF (2.2.13) application wired with CDI 2.0 (Weld 3.0.4). The app contains some real beans (frontend) and some business logic (backend).

Now the question is up to what part of the application CDI should be used?

  • Obviously the frontend beans shall be managed by CDI
  • My first guess was to inject the backend services (somehow wrapping them in an @ApplicationScoped scope) to those CDI-managed frontend beans
  • The backend then would be good old plain Java

I am a little bothered to use CDI in some parts of the application in in some not...

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
towi
  • 11
  • 1
  • Why wrapping back-end services in a (cdi?) managed application scoped bean, why not use EJB's or make then cdi themselves? Start reading here: https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao and see https://github.com/javaeekickoff/java-ee-kickoff-app – Kukeltje Jul 24 '18 at 13:45
  • I also thought about using CDI in the backend, but ran into troubles with object creation with arguments at runtime, sth like `new Product(String id)`? Had a look at the CDI producers as factories, but couldn't make them work with runtime arguments but only with compile time annotations? – towi Jul 25 '18 at 06:37
  • You must decide what you want to do at the backend. Is it transactional? is messaging? then ejb lends itself to. Good news is that ejb beans are integrated into CDI context per specs (as long as they are not remote beans), and hence normal `@Inject` works normally, but of course you can still use `@EJB` in CDI. If no transaction/messaging, i would use only CDI and ditch the ejb container altogether – maress Jul 25 '18 at 09:32
  • If going for pure CDI without EJBs: How to create the objects returned by the services? At some point I need to create objects with runtime parameters (e.g. IDs) Having a look at [http://docs.jboss.org/cdi/learn/userguide/CDI-user-guide.html#producer_methods](http://docs.jboss.org/cdi/learn/userguide/CDI-user-guide.html#producer_methods) I cannot figure out how to achieve that... – towi Jul 25 '18 at 12:48
  • You can really use CDI whenever it suits your model. As for dynamic creation, producer methods are your go-to probably (we don't know much about your structure and use case). In the body of the producer method, you just need to resolve your dynamic parameter(s) and then create a new object with it. – Siliarus Jul 31 '18 at 07:37

0 Answers0