[英]Accessing a EJB from a POJO
是否可以从POJO访问EJB? 我试过了
@EJB MyClass obj
但这不起作用。
如果您使用可以将任何内容转换为托管组件的JSR-199(Java C ontexts和D ependency I njection),则可以进行注入。 因此,如果您的POJO是CDI托管bean,您可以:
@Inject MyEjb service
没有CDI,您将不得不进行查找。
可以从POJO访问EJB。 你必须为此进行JNDI查找。
通常,依赖注入,就像您现在尝试的那样,仅适用于托管环境 - 在Servlet和EJB中。
您正在使用的注释要求您的基础结构知道使用该注释进入该字段。 您可能需要使用包装类或手动执行,这实际上取决于您的设置。
我将查看@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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.