JEE的@EJB和Spring的@Autowired有什么区别?

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

JEE@EJBSpring框架的@Autowired有什么区别?

我看到以下是 @EJB 的定义,它看起来真的很像 @Autowired 的定义:

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.

@EJB 用于将 EJB 注入另一个 EJB,在 JEE世界.
参见:Should I use @EJB or @Inject

Spring中,等效注入是通过使用@Autowired.
完成的 示例:

@Service
public class MyServiceImpl implements MyService {

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

@Service是一个Spring注解,用于注解服务层的class。
其他注释:@Component@Repository@Controller@Service
参见:What's the difference between @Component, @Repository & @Service annotations in Spring?

我会说:

  • @Component@Repository@Controller@Service 更接近于 @Stateless@Statefull,以及
  • @EJB 类似于 @Autowired

更新 @Autowired 告诉 Spring 框架为您找到依赖项。 @Inject 注解也有同样的作用,但它们之间的主要区别在于 @Inject 是依赖注入的标准注解,而 @Autowired 是 spring 特定的。 参见:https://javarevisited.blogspot.com/2017/04/difference-between-autowired-and-inject-annotation-in-spring-framework.html