请问下有Ejb3+Spring3+SpringMVC的例子嘛,或者讲讲他们之间怎么调用?我们的项目情况如下:
Spring3是一个工程ejb3又是一个工程用的jboss5发布的听说是Spring里面的Service调用EJB里面的service
我搞不懂这之间的关系,
求大神给个源码,或者文档啥的,学习下,谢谢了。
Spring3是一个工程ejb3又是一个工程用的jboss5发布的听说是Spring里面的Service调用EJB里面的service
我搞不懂这之间的关系,
求大神给个源码,或者文档啥的,学习下,谢谢了。
解决方案 »
- struts2 从action中读的内容在jsp中显示不出来
- 把网站设置桌面快捷方式
- hibernate 左连接求救,急啊。。。
- 如何使IIS6.0+tomcat整合
- 我的classes文件夹下的*.java文件怎么都没有了
- 一个不太留意的基础问题.现在才知道自己的基础差啊.
- struts+hibernate+spring快速入门例子,有需要的进来.....
- 请问各位大侠:java中标记为synchronized的方法A,那么对于A所调用的其他非synchronized的方法是否也并发执行
- 在类中取得当前文件所在的相对路径与绝对路径
- 无法访问云服务器上的nginx
- 马士兵spring教程疑问
- 利用UDP实现TCP 的功能
很简单的
定义一个ejb 远程接口
@Remote
public interface PCBService{}@Stateless(mappedName = "service.xxService")
@LocalBean
public class xxBean implements xxService{}
在spring的service里
用
@Resource(mappedName = "service.xxService")注入就好了。
也可以用jndi,
spring配置文件。
但是ejb要是远程接口哦,即使是同一个jvm,
EjbTools, 初始化JNDI, 根据EJB的JNDI名称获取EJB
package com.wanghy.j2ee;import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;import javax.ejb.EJBObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;public class EjbTools { private static final String PROPERTY_FILE_NAME = "EjbTools.properties";
private static Properties props; private synchronized void loadProperties() throws IOException {
if(props == null){
java.io.InputStream inputstream = ClassLoader.getSystemClassLoader().getResourceAsStream(PROPERTY_FILE_NAME);
if (inputstream == null){
inputstream = this.getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE_NAME);
if (inputstream == null){
throw new IOException("Read properties file error:" + PROPERTY_FILE_NAME);
}
}
props = new Properties();
props.load(inputstream);
}
}
public InitialContext getInitialContext() throws NamingException, IOException {
loadProperties();
Properties properties = new Properties();
properties.put("java.naming.factory.initial", props.getProperty("jndi.initialContextFactory"));
properties.put("java.naming.provider.url", props.getProperty("jndi.providerUrl"));
properties.put("java.naming.security.principal", props.getProperty("jndi.securityPrincipal"));
properties.put("java.naming.security.credentials", props.getProperty("jndi.securityCredentials"));
InitialContext initialcontext = new InitialContext(properties);
return initialcontext;
}
public static EJBObject getEJBObject(String s) throws NamingException, IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ EjbTools tool = new EjbTools();
InitialContext initialcontext = tool.getInitialContext();
Object home = initialcontext.lookup(s);
Class homeClass = home.getClass();
home = PortableRemoteObject.narrow(home, homeClass);
Method method = homeClass.getMethod("create", null);
EJBObject ejb = (EJBObject)method.invoke(home, null);
return ejb; }}
OrganApTools.java 获取组织管理EJB的工具类,主要是为了处理异常package com.ssh.organ;import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import javax.naming.NamingException;import com.wanghy.j2ee.*;public class OrganApTools { static OrganBusinessLogic getEjb() throws BeatRuntimeException, RemoteException{
try {
return (OrganBusinessLogic)EjbTools.getEJBObject("com.ssh.organ.OrganBusinessLogicHome");
} catch (SecurityException e) {
String msg = "SecurityException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (IllegalArgumentException e) {
String msg = "IllegalArgumentException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (NamingException e) {
String msg = "NamingException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (IOException e) {
String msg = "IOException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (NoSuchMethodException e) {
String msg = "NoSuchMethodException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (IllegalAccessException e) {
String msg = "IllegalAccessException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (InvocationTargetException e) {
String msg = "InvocationTargetException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
}
}}测试类, 也可以在service类里使用这样的代码获取EJB进行调用。 public static void main(String[] args) {
try { OrganBusinessLogic logicBean = OrganApTools.getEjb();
System.out.println("SessionBean:" + "com.ssh.organBusinessLogicBean");
System.out.println("执行开始");
long s = System.currentTimeMillis(); OrganBusinessData bizData = new OrganBusinessData(); long e = System.currentTimeMillis();
System.out.println("方法正常结束");
System.out.println("执行时间:" + (e-s)/1000d + "秒\r\n"); } catch (Exception e) {
e.printStackTrace();
} finally { }
}
EjbTools, 初始化JNDI, 根据EJB的JNDI名称获取EJB
package com.wanghy.j2ee;import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;import javax.ejb.EJBObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;public class EjbTools { private static final String PROPERTY_FILE_NAME = "EjbTools.properties";
private static Properties props; private synchronized void loadProperties() throws IOException {
if(props == null){
java.io.InputStream inputstream = ClassLoader.getSystemClassLoader().getResourceAsStream(PROPERTY_FILE_NAME);
if (inputstream == null){
inputstream = this.getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE_NAME);
if (inputstream == null){
throw new IOException("Read properties file error:" + PROPERTY_FILE_NAME);
}
}
props = new Properties();
props.load(inputstream);
}
}
public InitialContext getInitialContext() throws NamingException, IOException {
loadProperties();
Properties properties = new Properties();
properties.put("java.naming.factory.initial", props.getProperty("jndi.initialContextFactory"));
properties.put("java.naming.provider.url", props.getProperty("jndi.providerUrl"));
properties.put("java.naming.security.principal", props.getProperty("jndi.securityPrincipal"));
properties.put("java.naming.security.credentials", props.getProperty("jndi.securityCredentials"));
InitialContext initialcontext = new InitialContext(properties);
return initialcontext;
}
public static EJBObject getEJBObject(String s) throws NamingException, IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ EjbTools tool = new EjbTools();
InitialContext initialcontext = tool.getInitialContext();
Object home = initialcontext.lookup(s);
Class homeClass = home.getClass();
home = PortableRemoteObject.narrow(home, homeClass);
Method method = homeClass.getMethod("create", null);
EJBObject ejb = (EJBObject)method.invoke(home, null);
return ejb; }}
OrganApTools.java 获取组织管理EJB的工具类,主要是为了处理异常package com.ssh.organ;import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import javax.naming.NamingException;import com.wanghy.j2ee.*;public class OrganApTools { static OrganBusinessLogic getEjb() throws BeatRuntimeException, RemoteException{
try {
return (OrganBusinessLogic)EjbTools.getEJBObject("com.ssh.organ.OrganBusinessLogicHome");
} catch (SecurityException e) {
String msg = "SecurityException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (IllegalArgumentException e) {
String msg = "IllegalArgumentException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (NamingException e) {
String msg = "NamingException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (IOException e) {
String msg = "IOException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (NoSuchMethodException e) {
String msg = "NoSuchMethodException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (IllegalAccessException e) {
String msg = "IllegalAccessException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
} catch (InvocationTargetException e) {
String msg = "InvocationTargetException=" + e.getMessage();
XtLogger logger = new XtLogger(new OrganApTools(), "Organ");
throw new BeatRuntimeException(e, logger, "", "getEjb", msg);
}
}}测试类, 也可以在service类里使用这样的代码获取EJB进行调用。 public static void main(String[] args) {
try { OrganBusinessLogic logicBean = OrganApTools.getEjb();
System.out.println("SessionBean:" + "com.ssh.organBusinessLogicBean");
System.out.println("执行开始");
long s = System.currentTimeMillis(); OrganBusinessData bizData = new OrganBusinessData(); long e = System.currentTimeMillis();
System.out.println("方法正常结束");
System.out.println("执行时间:" + (e-s)/1000d + "秒\r\n"); } catch (Exception e) {
e.printStackTrace();
} finally { }
}
谢谢 非常感谢
2.配置jndi
3.Spring的service中调用ejb