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();
}
}
}