1

A lot of web resources say that Spring Bean is similar to EJB. The Spring bean also as EJB can define the logic via @Component, @Bean and @Service.

But EJBs cannot be run in servlet container like Tomcat.

What is key difference between EJB and Spring Beans?

pirho
  • 11,565
  • 12
  • 43
  • 70
j6wj1997
  • 179
  • 4

1 Answers1

2

It is not that much about what is the difference between Spring bean and EJB but how beforementioned are managed.

Plain EJBeans or Spring beans do not have any special magic or functionality themselves. The functionality related is implemented in the environment that beans are run.

When you run Spring application in Tomcat you actually run bunch of stuff from Spring framework that makes Spring beans work so not only the beans but lots of other code also that makes autowiring of services & components etc.. happen.

However when you implement JavaEE with EJB all of the stuff you deploy consists only of your business code and beans not anything like Spring that has the managing logic also included.

So EJB / JavaEE you need this managing stuff separately and this is why plain Tomcat is not enough for EJB. You need an J2EE container in which you run your EJBs for example - TomEE that is an extension having OpenEJB with Tomcat or see a list of Certified referencing runtimes (about in the middle of the Wikipage behind link).

See also this and this related more or less.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • Thanks fyi, I have a question. Can I run EJBeans in the spring enviroment in the simple tomcat server ? – j6wj1997 Apr 05 '19 at 14:39
  • @j6wj1997 I'm not an expert about the differences - even worked with both - but I know that - assuming your beans are configured by annotations - that Spring supports J2EE annotations to some extent if you add j2ee-api dependency to your project. And if not - having the source code - you can also try to convert the annotations from J2EE to Spring ones if not supported. Anyway you should study, try it yourself and make a new question with a proper example if you do not have success by yourself. – pirho Apr 08 '19 at 16:35