Java – dependency injection EJB 3 – too many choices?
We are starting a new project based on EJB 3.0 I have a "spring" background (and love it), so loose coupling and testability are a great necessity for me This post should not be about "EJB vs. spring" If you already have real project experience, it will be perfect
Here are some sample code to illustrate the problem:
Client – > EJB – > collaborator 1 – > collaborator Partner n
<!-- language: java --> @Stateless public class SampleService { // or @Inject via CDI // or @Autowired via Spring @EJB // or just use a stateless session bean via EJB 3.0 private Bank bank; // same for this component @EJB private Calculator calc; // both collaborators must be settable from outside,to make everything testable (and mockable) /** * sample "business service" called from client */ public void debit(BigDecimal amount){ calc.calculate(amount.subtract(new BigDecimal(100))); bank.debit(amount); } } // or via @Component (Spring),or CDI? @Stateless // or Stateless Session bean with optional @Service/@Singleton annotation? public class Calculator { public void calculate(BigDecimal subtract) { // calculate stuff.... } } // or via @Component (Spring),or CDI? @Stateless // or Stateless Session bean with optional @Service/@Singleton annotation? public class Bank { public void debit(BigDecimal amount) { // ... } }
I wonder what is the best way to implement dependency injection for all collaborators and their collaborators in EJB 3.0? In this sense, collaborators can take very, very small professional courses
So far, we have discussed the following options, because there is always no correct conclusion:)
>Only use EJB standard with all beeing stateless session beans and all consequences (such as pool, resource processing, etc.) > use stateless session beans as "business components" (entry points) and from there
a) Spring wired dependency (integrated through "JBoss snowdrop")
b) CDI wired dependency (via weld for EJB 3.0 and JBoss EAP 5.1)
I don't need to know how to use beans in unit tests My future answer is what is the best way to connect all dependencies in the running app server (spring vs. Guice vs. CDI vs. EJB) I just need to know the graph from the external EJB ("business entry point") So everything outside (servlets, frontend, etc.) is not the scope of this problem:)
Assume EJB 3.0 and JBoss EAP 5.1 are project settings:)
We look forward to your answer and hope to have some project - based knowledge
Solution
There are usually "too many choices" in Java, so it is the same in this regard I will not describe EJB as a generic dependency injection framework, but use it for its purpose If this is the code you want, you should add a framework for it If you know and like spring, go I use Guice and EJB (here is a good recipe) and the effect is also good. If you need another framework to figure out how to do this