I was reading the JavaEE 6 tutorial and, while reading the SessionBean and CDI sections, I came across several doubts.
1) For what I understood, the @EJB
annotation injects a SessionBean resulting in a use of the Dependency Inject pattern. I understand that this pattern aims to reverse the responsability of who builds what objects. So, instead of a certain class creating it owns dependencies, it will receive them in the constructor. However, how does the @EJB
annotation mitigates the problems of not injecting dependencies? The same goes for the @Inject
annotation.
2) I have this utility class (contains only static methods) that formats a date into several formats (yyyy-MM-dd, dd-MM-yyyy, etc...). Is it better to use a Stateless Session Bean for these methods or should I keep the Utility class? In case of using an EJB for this, what is the difference between using it or using a bean by using the @Inject
annotation?
3) When using Dependency Injection, does it make sense to use a Service Locator or Factory patterns? (Although I've seen Service Locater being documented as an anti-pattern).