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就只好自己写一个了。

Java代码   收藏代码
  1. package com.cyf.spring.ext;  
  2.   
  3.   
  4.   
  5. import javax.naming.Context;  
  6. import javax.naming.InitialContext;  
  7. import javax.naming.NamingException;  
  8.   
  9. import org.apache.log4j.Logger;  
  10. import org.springframework.beans.factory.FactoryBean;  
  11. import org.springframework.jndi.JndiTemplate;  
  12.   
  13. public class SlsbFactoryBean implements FactoryBean {  
  14.     
  15.    private static Logger log=Logger.getLogger(SlsbFactoryBean.class);  
  16.    private JndiTemplate jndiTemplate;  
  17.     private Context ejbCtx;  
  18.     private String jndiName;  
  19.     private Object ejbobj;  
  20.     public synchronized Object getEjbObject(){  
  21.         if (ejbobj==null){  
  22.             if (jndiTemplate == null || jndiTemplate.getEnvironment() == null) {  
  23.                 throw new LookupSlsbException("jndiTemplate is required!");  
  24.             }  
  25.             try {  
  26.                 ejbCtx = new InitialContext(jndiTemplate.getEnvironment());  
  27.             } catch (Exception e) {  
  28.                 throw new LookupSlsbException(  
  29.                         "can't initial a javax.naming.InitialContext ");  
  30.             }  
  31.             if (jndiName==null){  
  32.                 throw new LookupSlsbException("jndiName is required!");  
  33.             }  
  34.             try {  
  35.                 ejbobj=ejbCtx.lookup(jndiName);  
  36.                 return ejbobj;  
  37.             } catch (NamingException e) {  
  38.                 // TODO Auto-generated catch block  
  39.                 e.printStackTrace();  
  40.                 return null;  
  41.             }  
  42.         }else{  
  43.             return ejbobj;  
  44.         }  
  45.     }            
  46.     public Object getObject() throws Exception {  
  47.         // TODO Auto-generated method stub  
  48.         return getEjbObject();        
  49.     }  
  50.   
  51.     public Class getObjectType() {  
  52.         // TODO Auto-generated method stub  
  53.         return null;  
  54.     }  
  55.   
  56.     public boolean isSingleton() {  
  57.         // TODO Auto-generated method stub  
  58.         return false;  
  59.     }  
  60.   
  61.     public JndiTemplate getJndiTemplate() {  
  62.         return jndiTemplate;  
  63.     }  
  64.   
  65.     public void setJndiTemplate(JndiTemplate jndiTemplate) {  
  66.         log.info("setting jndiTemplate....");  
  67.         this.jndiTemplate = jndiTemplate;  
  68.     }  
  69.   
  70.     public String getJndiName() {  
  71.         return jndiName;  
  72.     }  
  73.   
  74.     public void setJndiName(String jndiName) {  
  75.         log.info("setting jndiName");  
  76.         this.jndiName = jndiName;  
  77.     }  
  78.   
  79.   
  80. }  

这个类实现了Spring的FactoryBean接口

Java代码   收藏代码
  1. /* 
  2.  * Copyright 2002-2007 the original author or authors. 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package org.springframework.beans.factory;  
  18.   
  19. /** 
  20.  * Interface to be implemented by objects used within a {@link BeanFactory} 
  21.  * which are themselves factories. If a bean implements this interface, 
  22.  * it is used as a factory for an object to expose, not directly as a bean 
  23.  * instance that will be exposed itself. 
  24.  * 
  25.  * <p><b>NB: A bean that implements this interface cannot be used as a 
  26.  * normal bean.</b> A FactoryBean is defined in a bean style, but the 
  27.  * object exposed for bean references is always the object that it creates. 
  28.  * 
  29.  * <p>FactoryBeans can support singletons and prototypes, and can 
  30.  * either create objects lazily on demand or eagerly on startup. 
  31.  * 
  32.  * <p>This interface is heavily used within the framework itself, for 
  33.  * example for the AOP {@link org.springframework.aop.framework.ProxyFactoryBean} 
  34.  * or the {@link org.springframework.jndi.JndiObjectFactoryBean}. 
  35.  * It can be used for application components as well; however, 
  36.  * this is not common outside of infrastructure code. 
  37.  * 
  38.  * @author Rod Johnson 
  39.  * @author Juergen Hoeller 
  40.  * @since 08.03.2003 
  41.  * @see org.springframework.beans.factory.BeanFactory 
  42.  * @see org.springframework.aop.framework.ProxyFactoryBean 
  43.  * @see org.springframework.jndi.JndiObjectFactoryBean 
  44.  */  
  45. public interface FactoryBean {  
  46.   
  47.     Object getObject() throws Exception;  
  48.   
  49.       
  50.     Class getObjectType();  
  51.   
  52.       
  53.     boolean isSingleton();  
  54.   
  55. }  

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

再来看下Spring配置文件。

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.     <bean id="propertyConfigurer"  
  6.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  7.         <property name="location">  
  8.             <value>WEB-INF/classes/jndi.properties</value>  
  9.         </property>  
  10.     </bean>  
  11.     <bean id="jndiTemplate"  
  12.         class="org.springframework.jndi.JndiTemplate">  
  13.         <property name="environment">  
  14.             <props>  
  15.                 <prop key="java.naming.provider.url">  
  16.                     ${java.naming.provider.url}  
  17.                 </prop>  
  18.                 <prop key="java.naming.factory.initial">  
  19.                     ${java.naming.factory.initial}  
  20.                 </prop>  
  21.                 <prop key="java.naming.factory.url.pkgs">  
  22.                     ${java.naming.factory.url.pkgs}  
  23.                 </prop>  
  24.             </props>  
  25.         </property>  
  26.     </bean>  
  27.     <bean id="RemoteDAO" class="com.cyf.spring.ext.SlsbFactoryBean">  
  28.         <property name="jndiTemplate">  
  29.             <ref local="jndiTemplate" />  
  30.         </property>  
  31.         <property name="jndiName" value="CommonDAOImpl/remote" />  
  32.     </bean>  
  33. </beans>  

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

调用的例子:

Java代码   收藏代码
  1. package com.cyf.spring.ext;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.faces.context.FacesContext;  
  6.   
  7. import org.apache.log4j.Logger;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.web.jsf.FacesContextUtils;  
  10.   
  11. import com.book.business.CommonDAO;  
  12. import com.bookstore.admin.actions.PressAction;  
  13.   
  14. public class TestSlsbFactoryBean {  
  15.  //在JSF环境中得到SpringApplicationContext  
  16.  private static ApplicationContext ctx = FacesContextUtils  
  17.    .getWebApplicationContext(FacesContext.getCurrentInstance());  
  18.  private static Logger log = Logger.getLogger(TestSlsbFactoryBean.class);  
  19.   
  20.  public void testBean() {  
  21.   //getBean(RemoteDAO)返回的结果就是调用SlsbFactoryBean.getObject()方法返回的结果  
  22.   CommonDAO dao = (CommonDAO)ctx.getBean("RemoteDAO");  
  23.   List list = dao.getObjByEQL("select count(p) from Press p"null, -1, -1);  
  24.   long size = (Long) list.get(0);  
  25.   log.info(size);  
  26.  }  
  27. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值