EJB @Local 和 @Stateless 在一起

EJB @Local and @Stateless together

由于在一个 EJB 上使用了@Local 和@Stateless,我的作业被大学拒绝了。它是 validate/fix 传递对象的辅助 bean。 我认为用本地和无状态注释我的 ejb 是完全合法的。 有人可以向我解释为什么这会成为问题吗?

来自 javax.ejb.Local javadoc:

When used on the bean class, declares the local business interface(s) for a session bean. ... Use of the Local annotation is only required when the bean class does not implement only a single interface other than any of the following: java.io.Serializable; java.io.Externalizable; any of the interfaces defined in javax.ejb.

所以当你在beanclass上使用这个注解时,你需要将本地接口'class作为参数传递给这个注解。 如果你的 bean 公开了一个无接口视图,你应该用 @LocalBean 注释它。来自 javax.ejb.LocalBean javadoc:

Designates that a session bean exposes a no-interface view. This annotation is required if a session bean exposes any other client views (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, Web Service) in addition to the no-interface view or its implements clause contains an interface other than java.io.Serializable; java.io.Externalizable; or any of the interfaces defined by the javax.ejb package.

所以,如果你的 bean 没有实现任何接口,你可以只用 @Stateless:

注释它
@Stateless
public class MyEJB {
    public void localMethod() {}
}

如果您的 EJB 方法被本地客户端使用(即,如果 ejb 客户端与要部署 ejb 会话 bean 的环境相同。)您必须提取由 @Local 注释的接口以便公开您的业务方法。

示例:

@Stateless
public class MyEJB implements MyEJBLocal{
    public void myBusinessMethod(){
        //Implementation
    }
}

@Local
public interface MyEJBLocal{
    public void myBusinessMethod();
}