请问下有Ejb3+Spring3+SpringMVC的例子嘛,或者讲讲他们之间怎么调用?
请问下有Ejb3+Spring3+SpringMVC的例子嘛,或者讲讲他们之间怎么调用?
我们的项目情况如下:
Spring3是一个工程
ejb3又是一个工程
用的jboss5发布的
听说是Spring里面的Service调用EJB里面的service
我搞不懂这之间的关系,
求大神给个源码,或者文档啥的,学习下,谢谢了。
[解决办法]
srping调用ejb,
很简单的
定义一个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,
[解决办法]
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;
}
}
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);
}
}
}
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 {
}
}