请问下有Ejb3+Spring3+SpringMVC的例子嘛,或者讲讲他们之间怎么调用?我们的项目情况如下:
Spring3是一个工程ejb3又是一个工程用的jboss5发布的听说是Spring里面的Service调用EJB里面的service
我搞不懂这之间的关系,
求大神给个源码,或者文档啥的,学习下,谢谢了。

解决方案 »

  1.   

    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,
      

  2.   

    ejb 要通过JNDI调用的,想要代码,等晚上发给你
      

  3.   


    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 {        }
        }
      

  4.   


    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 {        }
        }
    谢谢 非常感谢
      

  5.   

    1.配置数据源
    2.配置jndi
    3.Spring的service中调用ejb