使用@EJB注入或每次在Servlet过滤器中执行查找

时间:2012-09-24 12:19:43

标签: java java-ee orm ejb-3.0

我想将EJB(Local)注入Servlet过滤器,EAR部署在Weblogic 11g上。

我的问题是:

@EJB Annotation是否适用于Weblogic 11g或者它将被忽略? 要么 我必须查看如下所示并提及web.xml和weblogic xml文件中的引用:

Hashtable env = new Hashtable();
env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
Context ctx = new InitialContext( env );
ctx.lookup( "myejb" );

由于

1 个答案:

答案 0 :(得分:0)

我想支持如下

更多Rerference:JSF Application that uses managed-bean and ejb

远程和本地接口

    @Remote
    public interface IPersonEAORemote {
        public void show();
    }

    @Local
    public interface IPersonEAOLocal {
        public void show();
    }

EJB Bean

    @Stateless(name = "PersonEAO", mappedName = "PersonEAO")
    public class PersonEAOBean implements IPersonEAORemote, IPersonEAOLocal {
        /* if you use JPA
        @PersistenceContext(name = "EJB_DEMO")
        private EntityManager em;
        */

        public void show() {
            System.out.println("Good Luck!");
        }
    }

简单客户端

    public class Client {
        private IPersonEAORemote personEAO;

        private void init() {
            try {
                final Context context = getInitialContext();
                personEAO = (IPersonEAORemote)context.lookup("PersonEAO#<your package name>.IPersonEAORemote");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        private static Context getInitialContext() throws NamingException {
            Hashtable env = new Hashtable();
            // WebLogic Server 10.x connection details
            env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
            return new InitialContext(env);
        }


        public void show() {
            personEAO.show());
        }

        public static void main(String[] args) {
            Client client = new Client();
            client.init();
            client.show();
        }
    }

ManageBean:假设你使用JSF,你的支持bean就在这里;

    public class JSFBackingBean {
        @EJB
        IPersonEAORemote personEAO;

        public void show() {
            personEAO.show();
        }
    }

对于JBoss 5:

    Context.INITIAL_CONTEXT_FACTORY ---> org.jnp.interfaces.NamingContextFactory
    Context.PROVIDER_URL ---->http://localhost:1099