3

是否可以从 POJO 访问 EJB?我试过了

@EJB MyClass obj

但这不起作用。

4

4 回答 4

7

如果您使用可以将任何东西变成托管组件的 JSR-199(Java 上下文和依赖注入),注入可能的。因此,如果您的 POJO 是 CDI 托管 bean,您可以这样做:

@Inject MyEjb service

如果没有 CDI,您将不得不进行查找。

也可以看看

于 2010-08-14T14:15:56.983 回答
0

可以从 POJO 访问 EJB。您必须为此进行 JNDI 查找。

通常,依赖注入,就像您现在尝试的那样,仅在托管环境中有效 - 在 Servlet 和 EJB 中。

于 2010-08-14T12:54:27.913 回答
0

您正在使用的注释要求您的基础设施知道使用该注释进入该字段。您可能需要使用包装类或手动执行,这实际上取决于您的设置。

于 2010-08-14T13:15:11.110 回答
0

我会查看@Inject,因为这听起来像是我正在寻找的解决方案,但是当我尝试它时它不起作用。我确定我错过了一些东西,但我尝试了一段时间并没有得到任何结果。紧迫的最后期限使我实施了以下帮助程序类,我认为如果他们有类似的问题,我会发布给任何人使用。

感谢您的回复。

public class InjectionHelper {

    private static final String DEPENDENCY_SEPERATOR = "/";

    private static Logger logger = Logger.getLogger(InjectionHelper.class);

    private static Map<Class<?>, Object> dependencies = null;

    private static List<Object> dependenciesList = null;

    private static Context baseContext = null;

    /**
     * Search for the dependency of the specified class.
     */
    public static final <T> T injectDependency(Class<T> dependencyClass){

        //initialise the dependencies
        if(dependenciesList == null){
            dependenciesList = new ArrayList<Object>();
            dependencies = new HashMap<Class<?>, Object>();
            try{
                baseContext = new InitialContext();
                populateDependencies(baseContext, new Stack<String>());
            }
            catch(Exception e){
                logger.error("Error populating dependencies", e);
            }
        }

        //We have seen this dependency before and can get it from the map
        if(dependencies.containsKey(dependencyClass)){
            return (T)dependencies.get(dependencyClass);
        }

        //Not seen the dependency so we must try and find it in the list 
        for(Object o: dependenciesList){
            if(dependencyClass.isInstance(o)){
                dependencies.put(dependencyClass, o);
                return (T)o;
            }
        }

        //We don't have the dependency
        return null;
    }

    /**
     * Traverse the InitialContext and extract all dependencies and store them in the map keyed by their class.
     *  
     * @param lookupNameStack
     */
    private static final void populateDependencies(Context ctx, Stack<String> lookupNameStack) {
        try {
            NamingEnumeration<Binding> list = ctx.listBindings("");

            while (list.hasMore()) {
               Binding item = list.next();

               //Get the name and object for the binding
               String lookupName = item.getName();
               Object objectBinding = item.getObject();

               //If a JavaGlobalJndiNamingObjectProxy this is a dependency we want to store
               if(objectBinding instanceof JavaGlobalJndiNamingObjectProxy){

                   //Based on our current position in the tree get the string representation
                   Iterator<String> lookupNameIterator = lookupNameStack.iterator();
                   String lookupPrefix = "";
                   while(lookupNameIterator.hasNext()){
                       lookupPrefix += lookupNameIterator.next();
                   }

                   //lookup the object and store in the map
                   try{
                       Object obj = baseContext.lookup(lookupPrefix+lookupName);
                       dependenciesList.add(obj);
                       logger.info("Found [" + obj.getClass() + "] Lookup [" + lookupPrefix + lookupName +"]");
                   }
                   catch(Exception e){
                       logger.info("Failed to find Lookup [" + lookupPrefix+lookupName + "]", e);
                   }
               }
               lookupNameStack.push(lookupName+DEPENDENCY_SEPERATOR);

               //If we find a context we can explore that branch
               if (objectBinding instanceof Context) {
                   populateDependencies((Context) objectBinding, lookupNameStack);
               }
               //Now we have traversed the branch we need to remove the last leaf
               lookupNameStack.pop();
           }

        } catch (NamingException ex) {
           logger.info("JNDI failure: ", ex);
        }
    }
}
于 2010-10-25T17:04:53.293 回答