日期:2014-05-20 浏览次数:20729 次
package serviceimpl; import java.util.List; import java.util.logging.Level; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import service.TUserFacadeLocal; import service.TUserFacadeRemote; import util.LogUtil; import entitys.TUser; @Stateless(mappedName="TUserFacadeBean") @Remote(TUserFacadeRemote.class) @Local(TUserFacadeLocal.class) public class TUserFacadeBean implements TUserFacadeLocal, TUserFacadeRemote { @PersistenceContext private EntityManager entityManager; public TUser findById(Integer id) { LogUtil.log("finding TUser instance with id: " + id, Level.INFO, null); try { TUser instance = entityManager.find(TUser.class, id); LogUtil.log("findById userName " + instance.getUserName(), Level.INFO, null); return instance; } catch (RuntimeException re) { LogUtil.log("find failed", Level.SEVERE, re); throw re; } } }
public class UserBean implements Serializable { private TUserFacade userService; public void setUserService(TUserFacade userService) { this.userService = userService; } public void findUser(int id){ TUser user = userService.findById(id); LogUtil.log("userName: " + user.getUserName(), Level.INFO, null); } public List<TUser> getAll() { return userService.findAll(0, 10); } public static void main(String[] args) { System.out.println("begin"); Properties props = new Properties(); props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory"); props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming"); props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"); props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost"); props.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); try { InitialContext ctx = new InitialContext(props); TUserFacade helloWorld = (TUserFacade) ctx.lookup("TUserFacadeBean"); TUser user = helloWorld.findById(1); System.out.println(user.getUserName()); } catch (NamingException e) { e.printStackTrace(); } } }
<jee:remote-slsb id="userServiceRemote" business-interface="service.TUserFacade" jndi-name="TUserFacadeBean"></jee:remote-slsb> <bean id="userBean" class="backbean.UserBean" scope="session"> <property name="userService" ref="userServiceRemote"/> </bean>