Jakarta ee 在带有RPC调度的GWTP项目中使用EJB
我们正在web/企业应用程序中使用GWTP和Guice。我们希望使用EJB将业务逻辑放在EAR的一个单独模块中。因为RPC调度在这里起作用,所以只有一个servlet是封装的。所以,并没有直接的方法来注入我们的EJB,至少不能不放松EJB的容器管理Jakarta ee 在带有RPC调度的GWTP项目中使用EJB,jakarta-ee,ejb,gwtp,Jakarta Ee,Ejb,Gwtp,我们正在web/企业应用程序中使用GWTP和Guice。我们希望使用EJB将业务逻辑放在EAR的一个单独模块中。因为RPC调度在这里起作用,所以只有一个servlet是封装的。所以,并没有直接的方法来注入我们的EJB,至少不能不放松EJB的容器管理 在我们的应用程序中有没有其他集成EJB的方法?还是我在这里遗漏了什么?因此,经过一些研究,我似乎有两种方法可以在GUI/RPC调度应用程序中使用EJB注入: 1.集中注射 EJB注入在GuiceServletContextListener或任何子类的
在我们的应用程序中有没有其他集成EJB的方法?还是我在这里遗漏了什么?因此,经过一些研究,我似乎有两种方法可以在GUI/RPC调度应用程序中使用EJB注入: 1.集中注射 EJB注入在
GuiceServletContextListener
或任何子类的名称中工作。例如,您可以通过@EJB
注释注入bean,然后将引用传递给getInjector()
方法中的模块:
import javax.ejb.EJB;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
public class GuiceServletConfig extends GuiceServletContextListener {
@EJB
private SampleBeanLocal sampleBean;
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServerModule(), new BeanModule(sampleBean));
}
}
2.自定义注释
另一种方法是创建自定义注释,然后在不可能的情况下使用该注释注入bean。已经描述了实现
由于代码示例中存在一些缺陷,我还将在此处发布我的解决方案:
注释
打字机监听器
import java.lang.reflect.Field;
导入com.google.inject.TypeLiteral;
导入com.google.inject.spi.typeconference;
导入com.google.inject.spi.TypeListener;
/**
*{@link GuiceEJB}注释的自定义Guice{@link TypeListener}。
*/
公共类GuiceEJBTypeListener实现TypeListener{
@凌驾
public void hear(TypeLiteral TypeLiteral,typeconference){
Class<?>clazz=typeLiteral.getRawType();
while(clazz!=null){
//遍历类的字段
for(字段:clazz.getDeclaredFields()){
if(field.isAnnotationPresent(GuiceEJB.class)){
注册(新GuiceEJBMemberInjector(字段));
}
}
//重复超级班
clazz=clazz.getSuperclass();
}
}
}
成员喷射器
import java.lang.reflect.Field;
导入javax.naming.Context;
导入javax.naming.InitialContext;
导入com.google.inject.membersInject;
/**
*{@link GuiceEJB}注释的自定义成员注入器。
*/
公共类GuiceEJBMemberInjector实现MembersInjector{
/**
*需要注入的字段。
*/
私人领域;
/**
*豆型。
*/
私有字符串类型;
/**
*豆名。
*/
私有字符串名称;
/**
*@param字段
*/
公共GuiceEJBMemberInjector(字段){
this.field=字段;
GuiceEJB annotation=field.getAnnotation(GuiceEJB.class);
this.type=annotation.type();
this.name=annotation.name();
此.field.setAccessible(true);
}
@凌驾
公众成员(T){
试一试{
if(“local”.compareTignoreCase(this.type)==0){
this.field.set(t,findLocalEJB());
}
}捕获(例外e){
抛出新的GuiceEJBInjectException(e);
}
}
/**
*查找给定字段的本地EJB。
*
*@返回
*/
私有对象findLocalEJB(){
Context initialContext=null;
对象localRef=null;
试一试{
//按名字查找这个豆子
localRef=InitialContext.doLookup(this.name);
}捕获(例外e){
抛出新的GuiceEJBInjectException(e);
}最后{
试一试{
if(initialContext!=null)
initialContext.close();
}捕获(例外e){
}
}
返回localRef;
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ TYPE, METHOD, FIELD})
public @interface GuiceEJB {
/**
* Name of the bean.
*
* @return Name of the bean.
*/
String name();
/**
* Local EJB as default type.
*
* @return bean type.
*/
String type() default "local";
}
import java.lang.reflect.Field;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
/**
* Custom Guice {@link TypeListener} for the {@link GuiceEJB} annotation.
*/
public class GuiceEJBTypeListener implements TypeListener {
@Override
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> encounter) {
Class< ? > clazz = typeLiteral.getRawType();
while (clazz != null) {
// iterate through the fields of the class
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(GuiceEJB.class)) {
encounter.register(new GuiceEJBMemberInjector<I>(field));
}
}
// repeat for super class
clazz = clazz.getSuperclass();
}
}
}
import java.lang.reflect.Field;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.google.inject.MembersInjector;
/**
* Custom member injector for {@link GuiceEJB} annotations.
*/
public class GuiceEJBMemberInjector<T> implements MembersInjector<T> {
/**
* Field that needs injection.
*/
private Field field;
/**
* Bean type.
*/
private String type;
/**
* Bean name.
*/
private String name;
/**
* @param field
*/
public GuiceEJBMemberInjector(Field field) {
this.field = field;
GuiceEJB annotation = field.getAnnotation(GuiceEJB.class);
this.type = annotation.type();
this.name = annotation.name();
this.field.setAccessible(true);
}
@Override
public void injectMembers(T t) {
try {
if ("local".compareToIgnoreCase(this.type) == 0) {
this.field.set(t, findLocalEJB());
}
} catch (Exception e) {
throw new GuiceEJBInjectException(e);
}
}
/**
* Lookup an local EJB for the given field.
*
* @return
*/
private Object findLocalEJB() {
Context initialContext = null;
Object localRef = null;
try {
// look up the bean by name
localRef = InitialContext.doLookup(this.name);
} catch (Exception e) {
throw new GuiceEJBInjectException(e);
} finally {
try {
if (initialContext != null)
initialContext.close();
} catch (Exception e) {
}
}
return localRef;
}
}