---
title: "EJB3学习记录"
author: "xyh"
date: "2009-06-20T14:31:15Z"
tags: ["Bean", "JMS", "Java", "Spring", "XML"]
slug: "ejb3"
draft: false
lastmod: "2009-06-20T14:31:15Z"
id: 412048
url: "https://xyh.iteye.com/blog/412048"
---

EJB3.0和Spring2.5都高度支持annotations开发，可以提高开发效率。 下面简单的记录spring如何访问SessionBean 先定义一个通用的接口：PersonDAO.java \[code="java"] public interface PersonDAO{ public boolean insert(Person person); public boolean update(Person person); public boolean delete(Integer id); public List queryList(); public Person queryDetail(Integer id); public String getNameByID(Integer id); } \[/code] 远程接口：PersonDAORemote.java \[code="java"] @Remote public interface PersonDAORemote extends PersonDAO{ } \[/code] 本地接口：PersonDAOLocal.java \[code="java"] @Local public interface PersonDAOLocal extends PersonDAO{ } \[/code] SessionBean：PersonDAOBean.java \[code="java"] @Stateless @LocalBinding(jndiBinding = "cn/edu/net/dao/PersonDAO/local") @RemoteBindings({@RemoteBinding(jndiBinding = "cn/edu/net/dao/PersonDAO/remote")}) public class PersonDAOBean implements PersonDAOLocal,PersonDAORemote{ @PersistenceContext(unitName="testPU") protected EntityManager em; ..... } \[/code] 在Web.xml加人spring支持： \[code="xml"] contextConfigLocation classpath\*:/applicationContext\*.xml org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener \[/code] 配置applicationContext.xml文件: \[code="xml"] Spring配置文件 org.jnp.interfaces.NamingContextFactory localhost:1099 org.jboss.naming:org.jnp.interfaces cn/edu/net/stateful/ShoppingCart/local \[/code] 在业务类就可以这样调用: PersonService.java: \[code="java"] @Service("personService") public class PersonService { @Autowired private PersonDAO personDao; ....... } \[/code] 一般系统开发中，绝大部分是stateless sessionbean,这种中粒度的slsb可重用性很好；相对来说，stateful sessionbean 用的比较少，一般小数据量的保存状态都直接放到httpsession里面去. MDB VS Stand-alone JMS clients，优点就在于the EJB container can instantiate multiple MDB instances to handle multiple messages simultaneously. A message-driven bean (MDB) acts as a JMS message listener. 就是说EJB容器可以实例化多个MDB，可以同时处理多个消息，MDB充当JMS消息的监听者 缺点呢： One limitation of message-driven beans compared to standard JMS listeners is that you can associate a given message-driven bean deployment with only one Queue or Topic 缺点在于一个指定的MDB只能绑定一个Queue，或者一个Topic.