如何将 EJB 会话注入 ADF ManagedBean
How to inject EJB Session to ADF ManagedBean
我正在研究 Oracle ADF 和 EJB。我将 ADF faces 用作视图和控制器,将 EJB 用作服务。我不想使用 ADF 模型绑定 EJB 会话以与 ADF 接口交互。因此,我创建了一个托管 bean 来与 ADF faces 进行交互
ManagedBean
public class EmployeeManagementController {
private List<Jobs> jobList;
private RichTable jobTable;
private RichPanelGroupLayout panelGroup;
//@EJB(mappedName = "HRSysDemo.JobBean",name = "jobBean")
@EJB
private JobBeanLocal jobBean;
/*...*/
public void initPage() {
System.out.println("TESTING . . .");
jobList = jobBean.getJobsFindAll();
}
}
会话 Bean
@Stateless(mappedName = "HRSysDemo/JobBean")
public class JobBean implements JobBeanRemote, JobBeanLocal {
@Resource
SessionContext sessionContext;
@PersistenceContext(unitName = "Model")
private EntityManager em;
/*...*/
public List<Jobs> getJobsFindAll() {
return em.createNamedQuery("Jobs.findAll").getResultList();
}
}
我的问题是 "jobBean" 总是得到 "null",这意味着 EJB Session 无法注入 ManagedBean。尝试了一些方法,比如改变接口注入(Remote interface),指定name和mappedName,但是还是不行。那么,如何将 EJB SessionBean 注入 ADF ManagedBean?
提前致谢!
您是否在 adfc-config 中定义了 bean?如果这样,它可能不起作用 - 尝试在常规的 faces-config 文件中定义它们。
我按照 here 中的一些步骤成功了,它运行不佳,但我已经修复了它们。我希望这对其他人有帮助:
这是我的代码:
ELResolverWithInjectEJB.java
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;
import com.google.common.collect.Lists;
import java.util.concurrent.ConcurrentHashMap;
import javax.el.ELContext;
import javax.el.MapELResolver;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import oracle.adf.controller.metadata.model.beans.ManagedBean;
import oracle.adf.controller.metadata.model.beans.ManagedBeanScopeType;
/* Illegal internal package import. Please use public API */
import oracle.adfinternal.controller.beans.AnnotationUtils;
import oracle.adfinternal.controller.beans.ManagedBeanFactory;
import oracle.adfinternal.controller.state.ScopeMap;
public class ELResolverWithInjectEJB extends MapELResolver {
private static Method getManagedBeanScopeType = null;
private static Method shouldIncludeHigherScopes = null;
private static Method newInstance = null;
static {
try {
getManagedBeanScopeType = ScopeMap.class.getDeclaredMethod("getManagedBeanScopeType", null);
getManagedBeanScopeType.setAccessible(true);
shouldIncludeHigherScopes = ScopeMap.class.getDeclaredMethod("shouldIncludeHigherScopes", null);
shouldIncludeHigherScopes.setAccessible(true);
newInstance = ManagedBeanFactory.class.getDeclaredMethod("newInstance", ManagedBean.class);
newInstance.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ELResolverWithInjectEJB() {
super();
}
@SuppressWarnings("oracle.jdeveloper.java.semantic-warning")
public Object getValue(ELContext elContext, Object base, Object var) {
ScopeMap scope = (ScopeMap) (base instanceof ScopeMap ? base : null);
if (scope == null || scope.containsKey(var))
return super.getValue(elContext, base, var);
Object bean = null;
try {
ManagedBeanFactory mbf = ManagedBeanFactory.getInstance();
ManagedBean managedBean = mbf.getManagedBeanInCurrentPageFlow((String) var);
ManagedBeanScopeType expectedScope = (ManagedBeanScopeType) getManagedBeanScopeType.invoke(scope, null);
if (((Boolean) shouldIncludeHigherScopes.invoke(scope, null)) &&
(managedBean == null || !managedBean.getScope().equals(expectedScope))) {
managedBean = mbf.getManagedBeanInAdfPageFlow((String) var);
}
if (managedBean != null && managedBean.getScope().equals(expectedScope)) {
bean = newInstance.invoke(mbf, managedBean);
if (bean != null) {
scope.put((String) var, bean);
EJBInjector.inject(bean);
AnnotationUtils.runPostConstructIfSpecified(bean, managedBean.getName(), managedBean.getScope());
elContext.setPropertyResolved(true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
static class EJBInjector {
private static Map<String, List> processed = Maps.newConcurrentMap();
private static Map<String, String> mappedBean = Maps.newConcurrentMap();
private static Context ctx = null;
static {
try {
ctx = getInitialContext();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void inject(Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
List<AccessibleObject> inj = null;
if (!processed.containsKey(o.getClass().getName())) {
inj = Lists.newArrayList();
for (Field field : o.getClass().getDeclaredFields()) {
if (field.getAnnotation(Autoinjector.class) != null) {
field.setAccessible(true);
inj.add(field);
Autoinjector rm = field.getAnnotation(Autoinjector.class);
mappedBean.put(convertKey(o, field), rm.ejbMappedName());
}
}
processed.put(o.getClass().getName(), inj.isEmpty() ? null : inj);
}
inj = inj != null ? inj : processed.get(o.getClass().getName());
if (inj != null) {
for (AccessibleObject i : inj) {
((Field) i).set(o,ctx.lookup(mappedBean.get(convertKey(o, (Field) i)) + "#" +
((Field) i).getType().getName()));
}
}
}
private static String convertKey(Object o, Field f) {
return o != null && f != null ? o.getClass().getName() + "_" + f.getType().getName() + "_" + f.getName() :
"";
}
@SuppressWarnings("unchecked")
public static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,[your_provide]);
env.put(Context.PROVIDER_URL, [your_url_server]);
return new InitialContext(env);
}
}
}
Autoinjector.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autoinjector {
String ejbMappedName() default "";
}
人脸-config.xml
<application>
<default-render-kit-id>oracle.adf.rich</default-render-kit-id>
<el-resolver>[your_package].ELResolverWithInjectEJB</el-resolver>
</application>
在控制器中
public class DicOrgDetailPageController extends BaseController {
private static final long serialVersionUID = 2332665929048532518L;
/* -- Services -- */
@Autoinjector(ejbMappedName = "mytest")
private SB_DicOrgService sbDicOrgService;
}
我正在研究 Oracle ADF 和 EJB。我将 ADF faces 用作视图和控制器,将 EJB 用作服务。我不想使用 ADF 模型绑定 EJB 会话以与 ADF 接口交互。因此,我创建了一个托管 bean 来与 ADF faces 进行交互
ManagedBean
public class EmployeeManagementController {
private List<Jobs> jobList;
private RichTable jobTable;
private RichPanelGroupLayout panelGroup;
//@EJB(mappedName = "HRSysDemo.JobBean",name = "jobBean")
@EJB
private JobBeanLocal jobBean;
/*...*/
public void initPage() {
System.out.println("TESTING . . .");
jobList = jobBean.getJobsFindAll();
}
}
会话 Bean
@Stateless(mappedName = "HRSysDemo/JobBean")
public class JobBean implements JobBeanRemote, JobBeanLocal {
@Resource
SessionContext sessionContext;
@PersistenceContext(unitName = "Model")
private EntityManager em;
/*...*/
public List<Jobs> getJobsFindAll() {
return em.createNamedQuery("Jobs.findAll").getResultList();
}
}
我的问题是 "jobBean" 总是得到 "null",这意味着 EJB Session 无法注入 ManagedBean。尝试了一些方法,比如改变接口注入(Remote interface),指定name和mappedName,但是还是不行。那么,如何将 EJB SessionBean 注入 ADF ManagedBean?
提前致谢!
您是否在 adfc-config 中定义了 bean?如果这样,它可能不起作用 - 尝试在常规的 faces-config 文件中定义它们。
我按照 here 中的一些步骤成功了,它运行不佳,但我已经修复了它们。我希望这对其他人有帮助:
这是我的代码:
ELResolverWithInjectEJB.java
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;
import com.google.common.collect.Lists;
import java.util.concurrent.ConcurrentHashMap;
import javax.el.ELContext;
import javax.el.MapELResolver;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import oracle.adf.controller.metadata.model.beans.ManagedBean;
import oracle.adf.controller.metadata.model.beans.ManagedBeanScopeType;
/* Illegal internal package import. Please use public API */
import oracle.adfinternal.controller.beans.AnnotationUtils;
import oracle.adfinternal.controller.beans.ManagedBeanFactory;
import oracle.adfinternal.controller.state.ScopeMap;
public class ELResolverWithInjectEJB extends MapELResolver {
private static Method getManagedBeanScopeType = null;
private static Method shouldIncludeHigherScopes = null;
private static Method newInstance = null;
static {
try {
getManagedBeanScopeType = ScopeMap.class.getDeclaredMethod("getManagedBeanScopeType", null);
getManagedBeanScopeType.setAccessible(true);
shouldIncludeHigherScopes = ScopeMap.class.getDeclaredMethod("shouldIncludeHigherScopes", null);
shouldIncludeHigherScopes.setAccessible(true);
newInstance = ManagedBeanFactory.class.getDeclaredMethod("newInstance", ManagedBean.class);
newInstance.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ELResolverWithInjectEJB() {
super();
}
@SuppressWarnings("oracle.jdeveloper.java.semantic-warning")
public Object getValue(ELContext elContext, Object base, Object var) {
ScopeMap scope = (ScopeMap) (base instanceof ScopeMap ? base : null);
if (scope == null || scope.containsKey(var))
return super.getValue(elContext, base, var);
Object bean = null;
try {
ManagedBeanFactory mbf = ManagedBeanFactory.getInstance();
ManagedBean managedBean = mbf.getManagedBeanInCurrentPageFlow((String) var);
ManagedBeanScopeType expectedScope = (ManagedBeanScopeType) getManagedBeanScopeType.invoke(scope, null);
if (((Boolean) shouldIncludeHigherScopes.invoke(scope, null)) &&
(managedBean == null || !managedBean.getScope().equals(expectedScope))) {
managedBean = mbf.getManagedBeanInAdfPageFlow((String) var);
}
if (managedBean != null && managedBean.getScope().equals(expectedScope)) {
bean = newInstance.invoke(mbf, managedBean);
if (bean != null) {
scope.put((String) var, bean);
EJBInjector.inject(bean);
AnnotationUtils.runPostConstructIfSpecified(bean, managedBean.getName(), managedBean.getScope());
elContext.setPropertyResolved(true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
static class EJBInjector {
private static Map<String, List> processed = Maps.newConcurrentMap();
private static Map<String, String> mappedBean = Maps.newConcurrentMap();
private static Context ctx = null;
static {
try {
ctx = getInitialContext();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void inject(Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
List<AccessibleObject> inj = null;
if (!processed.containsKey(o.getClass().getName())) {
inj = Lists.newArrayList();
for (Field field : o.getClass().getDeclaredFields()) {
if (field.getAnnotation(Autoinjector.class) != null) {
field.setAccessible(true);
inj.add(field);
Autoinjector rm = field.getAnnotation(Autoinjector.class);
mappedBean.put(convertKey(o, field), rm.ejbMappedName());
}
}
processed.put(o.getClass().getName(), inj.isEmpty() ? null : inj);
}
inj = inj != null ? inj : processed.get(o.getClass().getName());
if (inj != null) {
for (AccessibleObject i : inj) {
((Field) i).set(o,ctx.lookup(mappedBean.get(convertKey(o, (Field) i)) + "#" +
((Field) i).getType().getName()));
}
}
}
private static String convertKey(Object o, Field f) {
return o != null && f != null ? o.getClass().getName() + "_" + f.getType().getName() + "_" + f.getName() :
"";
}
@SuppressWarnings("unchecked")
public static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,[your_provide]);
env.put(Context.PROVIDER_URL, [your_url_server]);
return new InitialContext(env);
}
}
}
Autoinjector.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autoinjector {
String ejbMappedName() default "";
}
人脸-config.xml
<application>
<default-render-kit-id>oracle.adf.rich</default-render-kit-id>
<el-resolver>[your_package].ELResolverWithInjectEJB</el-resolver>
</application>
在控制器中
public class DicOrgDetailPageController extends BaseController {
private static final long serialVersionUID = 2332665929048532518L;
/* -- Services -- */
@Autoinjector(ejbMappedName = "mytest")
private SB_DicOrgService sbDicOrgService;
}