----------------------------------------------------------------------------------------------------------------------------------------------------------

 泡牛吧!

                                       希望越来越多的光棍能够泡到牛

-----------------------------------------------------------------------------------------------------------------------------------------------------------

第一个EJB+spring成功后的问题

ISBusinessExample16.java
package com.openv.spring;
public interface ISBusinessExample16 {
public String getStr(String args);
}
SBusinessExample16.java
package com.openv.spring;
public class SBusinessExample16 implements ISBusinessExample16 {

public String getStr(String args) {
return "Hello," + args + "!";
}
}
appcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sbe16"
class="com.openv.spring.SBusinessExample16"/>
</beans>
ISBusinessExample16.java
package com.openv.spring;
public interface ISBusinessExample16 {
public String getStr(String args);
}
SBusinessExample16Home.java
package com.openv.spring;
mport javax.ejb.EJBHome;

public interface SBusinessExample16Home extends EJBHome {
public SBusinessExample16Remote create() throws javax.ejb.CreateException,
java.rmi.RemoteException;
}
SBusinessExample16Remote.java
package com.openv.spring;
public interface SBusinessExample16Remote extends EJBObject {
public String getStr(String args) throws java.rmi.RemoteException;
}
SBusinessExample16Bean.java
package com.openv.spring;
import javax.ejb.CreateException;
import org.springframework.ejb.support.AbstractStatelessSessionBean;
public class SBusinessExample16Bean extends AbstractStatelessSessionBean
implements ISBusinessExample16 {
private ISBusinessExample16 sbe;

protected void onEjbCreate() throws CreateException {
sbe = (ISBusinessExample16) getBeanFactory().getBean("sbe16");
}

public String getStr(String args) {
return sbe.getStr(args);
}
}
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" version="2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>SBusinessExample16Bean</display-name>

<enterprise-beans>
<session>
<ejb-name>SBusinessExample16Bean</ejb-name>
<home>com.openv.spring.SBusinessExample16Home</home>
<remote>com.openv.spring.SBusinessExample16Remote</remote>
<ejb-class>com.openv.spring.SBusinessExample16Bean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>appcontext.xml</env-entry-value>
</env-entry>
</session>
</enterprise-beans>
</ejb-jar>
jboss.xml
<!DOCTYPE jboss PUBLIC
"-//JBoss//DTD JBOSS 4.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>SBusinessExample16Bean</ejb-name>
<jndi-name>SBusinessExample16Bean</jndi-name>
</session>
</enterprise-beans>
</jboss>
SBusinessExample16BeanTest.java 测试类
package com.openv.spring;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import junit.framework.TestCase;
public class SBusinessExample16BeanTest extends TestCase {
protected com.openv.spring.SBusinessExample16Home home;
protected Context getInitialContext() throws Exception {
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context ctx = new InitialContext(props);
return ctx;
}
protected com.openv.spring.SBusinessExample16Home getHome()
throws Exception {
Context ctx = this.getInitialContext();
Object o = ctx.lookup("SBusinessExample16Bean");
com.openv.spring.SBusinessExample16Home intf =
(com.openv.spring.SBusinessExample16Home) PortableRemoteObject
.narrow(o, com.openv.spring.SBusinessExample16Home.class);
return intf;
}
public static void main(String[] args) {
junit.swingui.TestRunner.run(SBusinessExample16BeanTest.class);
}

public SBusinessExample16BeanTest(String arg0) {
super(arg0);
}

protected void setUp() throws Exception {
super.setUp();
this.home = this.getHome();
}

protected void tearDown() throws Exception {
super.tearDown();
}
public void testGetStr() {
com.openv.spring.SBusinessExample16Remote instance;
String result;
try{
String param0 = "xxxxx";
instance=this.home.create();
result =instance.getStr(param0);
System.out.println(result);
}
catch(javax.ejb.CreateException c)
{
c.printStackTrace();
}
catch(java.rmi.RemoteException e)
{
e.printStackTrace();
}
}
}
appcontextclient.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="sbe16client"
class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">

<property name="jndiName">
<value>SBusinessExample16Bean</value>
</property>
<property name="resourceRef">
<value>false</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
<property name="businessInterface">
<value>com.openv.spring.ISBusinessExample16</value>
</property>

</bean>
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.provider.url">
jnp://localhost:1099
</prop>
<prop key="java.naming.factory.url.pkgs">
org.jboss.naming:org.jnp.interfaces
</prop>
</props>
</property>
</bean>
</beans>
SBusinessExample16Client.java 客服端
package com.openv.spring;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jndi.JndiObjectFactoryBean;
public class SBusinessExample16Client {
protected static final Log log = LogFactory.getLog(SBusinessExample16Client.class);
public static void client() {
Resource resource = new ClassPathResource("/com/openv/spring/appcontextclient.xml");
BeanFactory factory = new XmlBeanFactory(resource);
ISBusinessExample16 se = (ISBusinessExample16) factory.getBean("sbe16client");
log.info(se.getStr("nimda"));
}
}
SBusinessExample16ClientTest.java 客端测试类
package com.openv.spring;
import junit.framework.TestCase;
public class SBusinessExample16ClientTest extends TestCase {
p ublic static void main(String[] args) {
}
public SBusinessExample16ClientTest(String arg0) {
super(arg0);
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testClient() {
SBusinessExample16Client t=new SBusinessExample16Client();
t.client();
}
}
运行 环境 jboss4.0x ecilpse3.spring junit;
在这个EJB中,
SBusinessExample16BeanTest测试
client端测试,结果是成功的。
Clien是远程调用;
由于只有一台电脑,同时也只能一个jvm中运行
假如我要在两台机器上运行的的话 a 做服务器(jboss) b做客服机;
ISBusinessExample16 接口定义是错误的,因为在b机器的jvm中没有这个接口的存在,是是还要象数据库驱动一样打包后加载到b机器里边呀!
haohao   2005-12-30 15:10:54 评论:4   阅读:1179   引用:0
是这样的 @2005-12-31 15:56:49  haohao
我也感觉这样
因为我从来没有用ejb的东西
@2005-12-30 17:02:14  hofman
不是说错在哪儿,而是你对remote ejb,jndi的机制缺乏必要的了解。
请给我说说jndi的错误 @2005-12-30 16:16:40  haohao
可以告诉jndi的错误在哪儿吗?
jndi @2005-12-30 15:30:22  hofman
你对jndi的认识有误.

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

一切版权属于个人(转载例外)