开发ejb:
1、创建一个ejb project
2、编写代码(entity bean ; session bean)
①entity bean
@Entity public class User implements Serializable { @Id @GeneratedValue private int id; private String username; private String password; //getter setter方法 }
②session bean
接口:
public interface UserManager { public void addUser(User user); public void deleteUser(int id); } public interface UserManagerLocal { public void addUser(User user); public void deleteUser(int id); }
Bean
@Stateless(name="userManager") @Remote({UserManager.class}) @Local({UserManagerLocal.class}) public class UserManagerBean implements UserManager, UserManagerLocal { @PersistenceContext(unitName="testsse") private EntityManager em; public void addUser(User user) { em.persist(user); } public void deleteUser(int id) { User user = em.find(User.class, id); em.remove(user); } }
一、ejb与spring集成
1、ejb集成spring:spring管理jndi的查找
①创建一个java project
②引入spring. jar(一个)
③编写spring配置文件
applicationContext.xml(放到src目录下):
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="userManager/remote"></property> </bean> </beans>
注意:如果ejb打包成为ear的话,那么property name=“jndiName”的value要加上ear包的名称,如test/userManager/remote。如果是local,则把remote换成local。
2、测试代码
public class UserManagerTest { private static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testAddUser() { UserManager userManager = (UserManager)factory.getBean("userManager"); User user = new User(); user.setPassword("password"); user.setUsername("username"); userManager.addUser(user); } }
二、ejb与struts集成
1、ejb集成struts:只是加入了struts,并未对ejb产生影响。
①建立web project
②引入struts相关jar包(struts1.0)(放到web-inf下的lib文件夹中)
③编写web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2、测试代码
编写strutsaction:
public class EJBAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionFormform, HttpServletRequest request, HttpServletResponse response) throws Exception { InitialContext ict = new InitialContext(); //注意:因为要用到UserManagerLocal这个接口,所以,要引入开发的ejb发布成的jar包 UserManagerLocal userManager = (UserManagerLocal)ict.lookup("userManager/local"); UserTest user = new UserTest(); user.setUsername("username=zhangsan"); user.setPassword("password=mima"); userManager.addUser(user); request.setAttribute("user", user); return mapping.findForward("success"); } }
struts配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <action-mappings > <action path="/ejbAction" type="com.dcy.ejb.struts.EJBAction"> <forward name="success" path="/success.jsp" /> </action> </action-mappings> <message-resources parameter="com.dcy.ejb.struts.ApplicationResources" /> </struts-config>Jsp页面
<body> This is my JSP page. <br> <a href="ejbAction.do">测试struts调用</a> </body>