`
xyh
  • 浏览: 84348 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

EJB3学习记录

    博客分类:
  • Java
阅读更多
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支持:
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/applicationContext*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>


配置applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-lazy-init="true" default-autowire="byName">

	<description>Spring配置文件</description>
	
	<!-- JNDI对象  -->
	<bean id="simpleJndiTemplate" class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
				<prop key="java.naming.provider.url">localhost:1099</prop>
                <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
			</props>
		</property>
	</bean>
   
    <!-- 访问远程无状态SessionBean -->
	<bean id="abstractRemoteSlsb" abstract="true" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
    	<property name="jndiTemplate" ref="simpleJndiTemplate" />
    	<property name="lookupHomeOnStartup" value="true"/>
	</bean>
	
	<!-- 访问本地无状态SessionBean -->
	<bean id="abstractLocalSlsb" abstract="true" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
    	<property name="jndiTemplate" ref="simpleJndiTemplate" />
    	<property name="lookupHomeOnStartup" value="true"/>
	</bean>
	
	 <bean id="abstractSfsb"  abstract="true" class="org.springframework.jndi.JndiObjectFactoryBean">
      	<property name="jndiTemplate" ref="simpleJndiTemplate" />
     </bean>
	
      <!-- 实现了一个简单的stateless SessionBean-->
	<bean id="personDao" parent="abstractLocalSlsb">
		<property name="jndiName" value="cn/edu/net/dao/PersonDAO/local"/>
		<property name="businessInterface" value="cn.edu.net.dao.PersonDAO"/>
	</bean>
	
         <!-- 实现了一个简单的stateful SessionBean-->
	 <bean id="shoppingCart" parent="abstractSfsb">
	    <property name="jndiName">
            <value>cn/edu/net/stateful/ShoppingCart/local</value>
        </property>
	 </bean>

	
 	 <!-- 支持@Autowired -->
	 <context:annotation-config/>
	
	 <!-- 扫描Bean组件 -->
	 <context:component-scan base-package="cn.edu.net" />
</beans>


在业务类就可以这样调用:
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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics