EJB3学习记录

阅读更多
EJB3.0和Spring2.5都高度支持annotations开发,可以提高开发效率。
下面简单的记录spring如何访问SessionBean
先定义一个通用的接口:PersonDAO.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);
  
}


远程接口:PersonDAORemote.java
@Remote
public interface PersonDAORemote extends PersonDAO{
  
}


本地接口:PersonDAOLocal.java
@Local
public interface PersonDAOLocal extends PersonDAO{
  
}


SessionBean:PersonDAOBean.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;
        .....
}


在Web.xml加人spring支持:
	
		contextConfigLocation
		classpath*:/applicationContext*.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
		org.springframework.web.util.IntrospectorCleanupListener
	


配置applicationContext.xml文件:



	Spring配置文件
	
	
	
		
			
				org.jnp.interfaces.NamingContextFactory
				localhost:1099
                org.jboss.naming:org.jnp.interfaces
			
		
	
   
    
	
    	
    	
	
	
	
	
    	
    	
	
	
	 
      	
     
	
      
	
		
		
	
	
         
	 
	    
            cn/edu/net/stateful/ShoppingCart/local
        
	 

	
 	 
	 
	
	 
	 



在业务类就可以这样调用:
PersonService.java:
@Service("personService")
public class PersonService {
	
	@Autowired
	private PersonDAO personDao;
        .......
}


一般系统开发中,绝大部分是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.

你可能感兴趣的:(Java,JMS,Spring,Bean,XML)