Class EJB 中的实例化

Class instantiation inside EJB

以下代码是否违反了 EJB 3 规范?如果是这样,我该怎么做才能保留所需的功能?

我问的原因是 IntelliJ 14 产生了这些警告:

我要实例化的 class(es) 不是 EJB,只是一个 POJO,EJB 充当这些 POJO 的存储库(它们正在封装业务逻辑)。

@Stateless
public class MyBean {
    public SomeInterface createSomeClass(final Class<? extends AbstractSomeClass> someClass, final MyArgument argument) {
        try {
            final Constructor constructor = someClass.getDeclaredConstructor(argument.getClass());
            constructor.setAccessible(true);
            return (SomeInterface) constructor.newInstance(state);
        } catch (InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) {
            // TODO fix exception handling
            throw new RuntimeException(e);
        }
    }
}

感谢您的帮助。

此致,

西蒙

编辑

在我看来,EJB 3 规范中有一节回答了我问题的第一部分:

The enterprise bean must not attempt to query a class to obtain information about the declared members that are not otherwise accessible to the enterprise bean because of the security rules of the Java language. The enterprise bean must not attempt to use the Reflection API to access information that the security rules of the Java programming language make unavailable.

我以前从未听说过这个,也不明白这个规则背后的原因。有状态 EJB 可能是解决我的问题的替代方案,但对我来说太重了。

本段实际上只是 heavy-handed 对 EJB 的最低 Java 2 安全策略(EJB 3.2 规范的第 16.3 节)的重述。该规范不保证您的 EJB 将有权执行其操作。如果您没有在您的应用程序服务器中启用 Java 2 安全性,或者您已授予 EJB 这样做的权限,那么您应该没问题。 (当然,inspecting/changing 您不拥有的对象的状态的正常警告仍然适用。)

如果您无论如何都想避免警告,那么另一种方法可能是创建一个工厂接口:

interface AbstractSomeClassFactory<T> { T create(MyArgument a); }

...并将其传递给 EJB。我没有其他好主意。