is it correct that in spring I can inject my own beans , and in ejb3 I can inject only ejb3 beans ? if so how can ejb3 be a replacement to Spring?
2 Answers
In a Java EE environment, you can not only use EJB, but also CDI. see How do CDI and EJB compare? interact?

- 1
- 1

- 7,750
- 3
- 28
- 80
Besides the fact that you can use CDI for injecting different types of beans, what do you mean by "ejb3" beans and how those beans are not yours as in case of spring? Spring injects any kind of bean and you do that either by declaring it in XML (old approach) or by specifying an annotation (@Component, @Service, etc.). Which is the case also for EJB3 (you can use @Stateless instead of @Service, just to make an analogy). So, in JEE environment one can be replaced with the other (from this point of view, Spring has some advantages as it sets the grounds for fast development, providing additional helpers, libraries, frameworks on top of JEE specification - see Spring Data JPA for one). So, I think is a matter of how you design your application to use one or the other.

- 1,274
- 10
- 11
-
I mean with out CDI . With ejb3 only (no spring or CDI) I can not inject a POJO class or any class that doesn't inplement or inherit from ejb3 classes , right ? – yryrp Jun 27 '15 at 20:31
-
in EJB3 you no longer implement or inherit anything, you use annotations (like you do for Spring also to do the injection). Is just a different set of annotations. You transform an POJO into an EJB Stateless Session Bean by annotating it with @Stateless, that is all you have to do. – iullianr Jun 27 '15 at 20:32
-
rigth soory , but the bean I'm injecting eith ejb annotation must it self be annotate by Stateless for example. I can not inject no annotated class , no ? – yryrp Jun 27 '15 at 20:44
-
no, you cannot. But can you do it in spring without XML configuration (like most of the configuration is done now)? I think it all comes down to how you design the application. Of course, if you have a legacy app, which has some POJOs and you want to inject them but you cannot modify the code, yes spring allows you to do it by means of XML config. – iullianr Jun 27 '15 at 20:48
-
There is no point in saying "EJB without CDI", @yryrp, because if you're dealing with EJBs on any Java EE server, you've got CDI too -- it's a part of the specification. With CDI, you can inject POJOs, and no need for XML configuration. [Here is an example](http://www.adam-bien.com/roller/abien/entry/simplest_possible_pojo_injection_example). – DavidS Mar 22 '16 at 18:40