Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:15:40+00:00 2026-05-17T20:15:40+00:00

Is it possible to access an EJB from a POJO? I have tried the

  • 0

Is it possible to access an EJB from a POJO? I have tried the

@EJB MyClass obj

but that does not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:15:40+00:00Added an answer on May 17, 2026 at 8:15 pm

    I will look in to the @Inject as that sounds like the solution I am looking for but it didn’t work when I tried it. I am sure I am missing something but I tried for a while and didn’t get anywhere. A tight deadline lead me to implementing the below helper class which I thought I would post for anyone to use if they have similar issues.

    Thanks for the responses.

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible not to use Java EE Application client from Glassfish to access
Possible Duplicate: Access x86 COM from x64 .NET I have a COM object which
I was wondering how does javascript(if possible) access a variable that has the same
Is it possible to access an element on a Master page from the page
Is it possible to access hardware devices (web-cams, magnetic card readers, etc.) from within
Is it possible to access the data belonging to the apps that ship with
Is it possible to access struts2 variable in jsp scriptlet? If I have struts2
Possible Duplicate: Access return value from Thread.Start()'s delegate function public string sayHello(string name) {
Is it possible to access Flash's ExternalInterface from QWebView ? If so, how can
Is it possible to access a context param from a jar in lib or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.