Issue
What is the difference between @EJB
of JEE and @Autowired
of Spring Framework?
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.
Solution
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
Answered By - Ermal
Answer Checked By - Mildred Charles (JavaFixing Admin)