EJB3与Spring的集成

11 篇文章 0 订阅

http://www.iteye.com/topic/851288

ejb3集成spring,部署于tomcat(OpenEjb) ,jdk1.6

1 Tomcat本不支持部署EJB,通过向其安装OpenEjb,可使其支持。
   下载http://www.apache.org/dyn/closer.cgi/openejb/3.1.3/openejb.war
   将下载的openejb.war 放在Tomcat的安装目录 webapps下。
   启动Tomcat。
   在IE中输入:http://localhost:8080/openejb 即可看到openejb信息

  2  ejb3与spring的集成


  EJB bean Hello 定义:
@Local
public interface HelloLocal
{
    public void hello();
}

@Stateless
public class HelloBean implements Hello,HelloLocal
{
    public void hello()
    {
        System.out.println("Hello,world!");
       
    }  
}


EJB bean Calculator 定义
(在CalculatorImpl中调用调用EJB bean Hello、Spring bean Chat)

  @Remote
public interface CalculatorRemote {
   
    public int sum(int add1, int add2);
   
    public int multiply(int mul1, int mul2);

}

@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class CalculatorImpl implements CalculatorRemote, CalculatorLocal {
    @EJB private HelloLocal helloBean;
    @Autowired private Chat chat;
    public void setChat(@Qualifier("chat") Chat chat){
        this.chat=chat;
    }
    public int sum(int add1, int add2) {
        helloBean.hello();
        chat.chat();
        return add1+add2;
    }

    public int multiply(int mul1, int mul2) {
        return mul1*mul2;
    }

}

  Chat bean定义----spring:
public interface Chat
{
    public void chat();
}


@Service
public class ChatImpl implements Chat
{
    public ChatImpl(){
        System.out.println("==================initial =====ChatImpl=========");
    }
    @Override
    public void chat()
    {
        // TODO Auto-generated method stub
        System.out.println("==================chat==============");
        System.out.println("==================Nice to meet you!");
    }
}


ejb bean 通过SpringBeanAutowiringInterceptor.class载入spring 配置,从而调用spring bean。
SpringBeanAutowiringInterceptor默认配置文件为beanRefContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- START SNIPPET: annotations -->

  <bean id="factoryKey"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="spring-context.xml"/>
</bean>


</beans>


spring 配置文件为spring-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- START SNIPPET: annotations -->

  <context:annotation-config/>

  <bean id="chat" class="com.ejb.test.ChatImpl"/>

</beans>


3 部署
  将此EJB工程打成jar包,比如放在D:\Tomcat\ejb下(注:“D:\Tomcat\ejb”是我自己建的)
  然后打开:Tomcat\conf\openejb.xml,将</openejb>前的内容改为:
  
   <!--
#
#? The <Deployments> element can be used to configure file
#? paths where OpenEJB should look for ejb jars or ear files.
#
#? See http://openejb.apache.org/deployments.html
#
#? The below entry is simply a default and can be changed or deleted
<Deployments dir="apps/" />原来是这句,我们用不到,可以用下面的语句直接加载指定的ejb jar包。
-->

<Deployments jar="D:/Tomcat/ejb/OpenEjbTest.jar" />

重启Tomcat,在浏览器输入:http://127.0.0.1:8089/openejb/invokeobj.jsp ,即可看到自己的EJB Bean.

4 客户端调用
public class TestCalculator
{
    public static void main(String[] args) throws NamingException {
        try
        {
            Properties p = new Properties();
            //p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

            p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
            p.put("java.naming.provider.url", "http://localhost/openejb/ejb");
            InitialContext initialContext = new InitialContext(p);
            CalculatorRemote remote =(CalculatorRemote) initialContext.lookup("CalculatorImplRemote");//CalculatorImplRemote
             System.out.println(remote.sum(1, 1));
        }
        catch (javax.naming.NamingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }   
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值