Access EJB from POJO

Can I access EJB from POJO? I tried

@EJB MyClass obj

but it does not work.

+3
source share
4 answers

I will look at @Inject as this seems like the solution I am looking for, but it did not work when I tried. I'm sure something is missing, but I tried for a while and will not go anywhere. A difficult time led me to implement the helper class below, which I thought I would lay out for those who would use them if they had similar problems.

Thanks for answers.

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
source

IF, JSR-199 (Java C ontexts D ependency I njection), - . , POJO CDI bean, :

@Inject MyEjb service

CDI .

.

+7

EJB POJO. JNDI- .

, , , - ​​EJB.

0

The annotation you use requires your infrastructure to know in the field with this annotation. you may need to use the packaging class or do it manually, it really depends on your installation.

0
source

Source: https://habr.com/ru/post/1759338/


All Articles