1

What is the difference between @EJB of JEE and @Autowired of Spring Framework?

  • Are those the same thing?

  • And if not, which are the differencies?

I have seen the following as @EJB's definition which really looks like a @Autowired's one:

An enterprise bean (EJB) is a server-side component that encapsulates the business logic of an application.

The business logic is the code that fulfills the purpose of the application. It does not perform display of business data or perform operations directly on the database.

CodeSlave
  • 425
  • 6
  • 21
  • 2
    Spring and JEE are competitors. That's why they have some similarities. Usually you pick one or other. Like as when you want one car and need to pick between e.g. Audi or Toyota. Your question is like asking for similarities/differences of Audi and Toyota. – BalusC Nov 19 '19 at 12:14
  • Hi @BalusC and thanks for the reply. So you mean that they are the same thing or `@EJB` is more general than `@Service` ? – CodeSlave Nov 19 '19 at 12:17
  • 3
    If you use JEE, then you need `@EJB`. If you use Spring, then you need `@Service`. That's all. – BalusC Nov 19 '19 at 12:18
  • Hi @BalusC, I have just 100% understood the meaning of your sentences. I was talking about the analogy between them. Based on @Ermal answer, I finally end up to : In Spring: `@Service` is injected via `@Autowired` or `@Inject`. Respectively in JEE, `@Stateless` or `@Statefull` are injected via `@EJB` or `@Inject`. – CodeSlave Nov 21 '19 at 08:25
  • Having them mixed in one application indicates a problem though. Use the one or the other. – BalusC Nov 21 '19 at 09:05

1 Answers1

1

The @EJB is used to inject an EJB into another EJB, in the JEE world.
See: Should I use @EJB or @Inject

In Spring the equivalent injection is done by the use of @Autowired.
Example:

@Service
public class MyServiceImpl implements MyService {

}
@Controller
public class MyController{
    @Autowired MyService myService;
}

@Service is a Spring annotation for annotating a class at a service layer.
Other annotations: @Component, @Repository, @Controller, @Service.
See: What's the difference between @Component, @Repository & @Service annotations in Spring?

I would say:

  • @Component, @Repository, @Controller & @Service are closer to @Stateless or @Statefull, and
  • @EJB is similar to @Autowired

UPDATE @Autowired tells Spring framework to find dependecies for you. The @Inject annotation also serves the same purpose, but the main difference between them is that @Inject is a standard annotation for dependency injection and @Autowired is spring specific. See: https://javarevisited.blogspot.com/2017/04/difference-between-autowired-and-inject-annotation-in-spring-framework.html

CodeSlave
  • 425
  • 6
  • 21
Ermal
  • 441
  • 5
  • 19
  • Hi @Ermal and thanks for the response. So in Spring: `@Service` is injected via `@Autowired` or `@Inject`. Respectively in JEE, `@Stateless` or `@Statefull` are injected via `@EJB` or `@Inject`. This is totally different from @BalusC 's above comments. – CodeSlave Nov 20 '19 at 17:14
  • 1
    I don't think is the oposite. Seems just a straight forward answer to your question: If you use JEE you need `@EJB`, If you use Spring, then you need `@Service` (and other annotations). EJB and JEE are competitors! – Ermal Nov 20 '19 at 21:49
  • Ok, I understand that now. The reasoning that the JEE service layer is defined by `@Stateless` or `@Statefull` is right or both of them are too general like `@Component`. In other words is really a `@Service` class similar to `@Stateless` or `@Statefull` class ? – CodeSlave Nov 21 '19 at 09:16
  • Hi again @Ermal, do you know if exists any mapper between spring & jee annotations? – CodeSlave Dec 02 '19 at 07:59
  • I don't think your question makes sense. Each framework has his annotations to express a specific type inside its semantic domain, but not necessary that type (or same semantic meaning) can be present in the different framework. No mapper needed but you have to rethink all your application architecture. – Ermal Dec 02 '19 at 18:10