`
jamjar
  • 浏览: 14134 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

Spring访问EJB3.0的SessionBean方法

阅读更多

Spring2.*提供了一个访问EJB的方法,即

“org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean”和

“org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean”

两个FactoryBean,一个用来访问本地的,一个用来访问远程的无状态SessionBean。但是这两个FactoryBean

只能针对EJB2.0的无状态SessionBean,所以在配置是必须提供SessionBean的Home接口。而在EJB3.0中已经没有了Home接口(或者说被隐藏起来了)。为了访问EJB3.0的无状态Session Bean就只好自己写一个了。

package com.cyf.spring.ext;



import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.jndi.JndiTemplate;

public class SlsbFactoryBean implements FactoryBean {
  
   private static Logger log=Logger.getLogger(SlsbFactoryBean.class);
   private JndiTemplate jndiTemplate;
    private Context ejbCtx;
    private String jndiName;
    private Object ejbobj;
    public synchronized Object getEjbObject(){
    	if (ejbobj==null){
    		if (jndiTemplate == null || jndiTemplate.getEnvironment() == null) {
				throw new LookupSlsbException("jndiTemplate is required!");
			}
			try {
				ejbCtx = new InitialContext(jndiTemplate.getEnvironment());
			} catch (Exception e) {
				throw new LookupSlsbException(
						"can't initial a javax.naming.InitialContext ");
			}
			if (jndiName==null){
				throw new LookupSlsbException("jndiName is required!");
			}
			try {
				ejbobj=ejbCtx.lookup(jndiName);
				return ejbobj;
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
    	}else{
    		return ejbobj;
    	}
    }          
	public Object getObject() throws Exception {
		// TODO Auto-generated method stub
        return getEjbObject();		
	}

	public Class getObjectType() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isSingleton() {
		// TODO Auto-generated method stub
		return false;
	}

	public JndiTemplate getJndiTemplate() {
		return jndiTemplate;
	}

	public void setJndiTemplate(JndiTemplate jndiTemplate) {
		log.info("setting jndiTemplate....");
		this.jndiTemplate = jndiTemplate;
	}

	public String getJndiName() {
		return jndiName;
	}

	public void setJndiName(String jndiName) {
		log.info("setting jndiName");
		this.jndiName = jndiName;
	}


}

这个类实现了Spring的FactoryBean接口

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory;

/**
 * Interface to be implemented by objects used within a {@link BeanFactory}
 * which are themselves factories. If a bean implements this interface,
 * it is used as a factory for an object to expose, not directly as a bean
 * instance that will be exposed itself.
 *
 * <p><b>NB: A bean that implements this interface cannot be used as a
 * normal bean.</b> A FactoryBean is defined in a bean style, but the
 * object exposed for bean references is always the object that it creates.
 *
 * <p>FactoryBeans can support singletons and prototypes, and can
 * either create objects lazily on demand or eagerly on startup.
 *
 * <p>This interface is heavily used within the framework itself, for
 * example for the AOP {@link org.springframework.aop.framework.ProxyFactoryBean}
 * or the {@link org.springframework.jndi.JndiObjectFactoryBean}.
 * It can be used for application components as well; however,
 * this is not common outside of infrastructure code.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @since 08.03.2003
 * @see org.springframework.beans.factory.BeanFactory
 * @see org.springframework.aop.framework.ProxyFactoryBean
 * @see org.springframework.jndi.JndiObjectFactoryBean
 */
public interface FactoryBean {

	Object getObject() throws Exception;

	
	Class getObjectType();

	
	boolean isSingleton();

}

请注意FactoryBean的“getObject()”方法,这个方法将会在Spring客户端调用ApplicationContext.getBean()的时候被调用,或者说getObject()代理了getBean()方法,下面将会看一个例子。所以我在getObject()方法中去lookup EJB3.0的sessionbean。

再来看下Spring配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>WEB-INF/classes/jndi.properties</value>
		</property>
	</bean>
	<bean id="jndiTemplate"
		class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.provider.url">
					${java.naming.provider.url}
				</prop>
				<prop key="java.naming.factory.initial">
					${java.naming.factory.initial}
				</prop>
				<prop key="java.naming.factory.url.pkgs">
					${java.naming.factory.url.pkgs}
				</prop>
			</props>
		</property>
	</bean>
	<bean id="RemoteDAO" class="com.cyf.spring.ext.SlsbFactoryBean">
		<property name="jndiTemplate">
			<ref local="jndiTemplate" />
		</property>
		<property name="jndiName" value="CommonDAOImpl/remote" />
	</bean>
</beans>

SlsbFactoryBean只需要设置jndi属性模板和jndiName,不需要EJB2.0的home接口了

调用的例子:

package com.cyf.spring.ext;

import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.book.business.CommonDAO;
import com.bookstore.admin.actions.PressAction;

public class TestSlsbFactoryBean {
 //在JSF环境中得到SpringApplicationContext
 private static ApplicationContext ctx = FacesContextUtils
   .getWebApplicationContext(FacesContext.getCurrentInstance());
 private static Logger log = Logger.getLogger(TestSlsbFactoryBean.class);

 public void testBean() {
  //getBean(RemoteDAO)返回的结果就是调用SlsbFactoryBean.getObject()方法返回的结果
  CommonDAO dao = (CommonDAO)ctx.getBean("RemoteDAO");
  List list = dao.getObjByEQL("select count(p) from Press p", null, -1, -1);
  long size = (Long) list.get(0);
  log.info(size);
 }
}

 

 

分享到:
评论
1 楼 duooluu 2008-06-29  
这样做没必要吧
直接用JndiObjectFactoryBean
或者
<jee:jndi-lookup id="RemoteDAO"
   jndi-name="CommonDAOImpl/remote" 
   environment-ref="environment"/>

相关推荐

    EJB3.0无状态SessionBean例子

    里面包含本地接口的SessionBean和远程接口的SessionBean。

    EJB3.0开发Session Bean.rar

    EJB3.0开发Session Bean.rar EJB3.0开发Session Bean.rar

    EJB3.0开发Session Bean

    EJB3.0开发Session Bean

    ejb3.0入门图文教程

    ejb3.0入门图文教程,包括EJB3.0开发Entity.pdf、EJB3.0开发Message Driven Bean.pdf、EJB3.0开发Session Bean.pdf

    struts,spring,ejb3.0

    struts,spring,ejb3.0整合

    EJB3.0入门经典(PDF)

    《EJB3.0入门经典》是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。《EJB3.0入门经典》内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发...

    EJB3.0和Spring比较

    EJB3.0和Spring比较

    ejb3.0实例教程

    期待已久的 EJB3.0 最终规范已经发布了。虽然 EJB3.0 最终规范出来了一段时间,但对 EJB3.0 的应用还停留在介 绍之中,应用实例更是少之又少,所以作者拟写本书,以简单的实例展现 EJB3.0 的开发过程,希望对大家有所...

    精通EJB3.0 中文版 3/3

    罗时飞精通EJB3.0.zip.001 罗时飞精通EJB3.0.zip.002 罗时飞精通EJB3.0.zip.003 《精通EJB3.0》共分为4个部分:第一部分对EJB编程基础进行介绍,概要性地对EJB进行了阐述;第二部分重点关注EJB编程的具体内容和...

    精通EJB3.0 中文版 1/3

    罗时飞精通EJB3.0.zip.001 罗时飞精通EJB3.0.zip.002 罗时飞精通EJB3.0.zip.003 《精通EJB3.0》共分为4个部分:第一部分对EJB编程基础进行介绍,概要性地对EJB进行了阐述;第二部分重点关注EJB编程的具体内容和...

    EJB3.0开发Message Driven Bean

    EJB3.0开发Message Driven Bean

    EJB3.0中文文档

    EJB3.0中文文档,实例教程.EJB3.0中文文档,实例教程.EJB3.0中文文档,实例教程.EJB3.0中文文档,实例教程.

    EJB 3.0从入门到精通

    全书共分16章,内容依次包含了Java EE概述、EJB基础、搭建EJB环境、会话bean、持久化实体、持久化实体管理器、对象关系映射、JPQL查询、消息驱动bean、事务、提高EJB 3.0性能、从Web层访问EJB 3和EJB安全、EJB和Web...

    ejb3.0开发规范PDF

    ejb3.0开发规范,开发ejb3.0过程中的参考文档

    EJB 3.0入门经典 源码

    本书是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。  本书内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发技术的同时,精心设计了与...

    EJB3.0入门经典源代码.part1

    本书是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。  本书内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发技术的同时,精心设计了与...

    weblogic+ejb3.0例子

    weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0

    ejb3.0开发实例(java工程)

    ejb3.0开发实例,会话bean开发,完整代码,适合入门学者

    ejb 3.0 jar 包 很全 part1

    ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全

    EJB 3.0实例教程.pdf

    EJB 3.0实例教程.pdf EJB 3.0实例教程.pdf

Global site tag (gtag.js) - Google Analytics