package com.ejb3inaction.actionbazaar.buslogic;import javax.ejb.Stateless;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import com.ejb3inaction.actionbazaar.persistence.Bid;@Stateless//将POJO标记为会话beanpublic class PlaceBidBean implements PlaceBid {···public PlaceBidBean(){}public Bid addBid(Bid bid) {System.out.println("Adding bid, bidder ID=" + bid.getBidderId()+ ", item ID=" + bid.getItemId() + ", bid amount="+ bid.getBidPrice() + ".");return save(bid);}···}}···package ejb3inaction.example.buslogic;import javax.ejb.Local;import ejb3inaction.example.persistence.Bid;@Local//将EJB业务接口标记为本地的public interface PlaceBid {Bid addBid(Bid bid);}
- @Stateless注解通知EJB容器这个bean是无状态会话bean。容器自动的为bean提供这些服务并发控制、线程安全、池化和事务管理。此外,你可以添加适用于无状态bean的其他服务,比如透明安全和拦截器。
- @ Local注解通知容器可以通过接口访问PlaceBid EJB。因为EJB和servlet通常在同一个应用程序中协同操作,所以这种方式非常好。另一种方式是,你可以使用@Remote注解标记接口。在Java远程方法调用(RMI)的控制下提供对@Remote进行远程访问,这是从Java客户端进行远程访问的理想方式。
package ejb3inaction.example.buslogic;import javax.ejb.EJB;import ejb3inaction.example.persistence.Bid;public class PlaceBidClient {@EJB//注入PlaceBid EJB的实例private static PlaceBid placeBid;public static void main(String [] args) {try {Bid bid = new Bid();bid.setBidderId("npanda");bid.setItemId(Long.valueOf(100));bid.setBidPrice(20000.40);System.out.println("Bid Successful, BidId Received is:" +placeBid.addBid(bid).getBidId());} catch (Exception ex) {ex.printStackTrace();}}}
- 首先,容器确保客户端可以跨越一次以上的方法调用访问分配到容器的bean,比如打电话,如果你在给定的时间(此时间就是会话)内给技术支持打电话的次数超过1次,它能确保你和同一个客服代表通话。
- 其次,容器确保在会话过程中维护bean实例的变量值,无需你编写任何会话维护代码。
package com.ejb3inaction.actionbazaar.buslogic;import java.util.ArrayList;import java.util.List;import javax.ejb.*;@Stateful//使用POJO有状态public class PlaceOrderBean implements PlaceOrder {private Long bidderId;private List<Long> items;private ShippingInfo shippingInfo;private BillingInfo billingInfo;public PlaceOrderBean() {items = new ArrayList<Long>();}public void setBidderId(Long bidderId) {this.bidderId = bidderId;}public void addItem(Long itemId) {items.add(itemId);}public void setShippingInfo(ShippingInfo shippingInfo) {this.shippingInfo = shippingInfo;}public void setBillingInfo(BillingInfo billingInfo) {this.billingInfo = billingInfo;}@Remove//包含删除方法public Long confirmOrder() {Order order = new Order();order.setBidderId(bidderId);order.setItems(items);order.setShippingInfo(shippingInfo);order.setBillingInfo(billingInfo);saveOrder(order);billOrder(order);return order.getOrderId();}···}···package com.ejb3inaction.actionbazaar.buslogic;import javax.ejb.Remote;@Remote//定义远程业务接口public interface PlaceOrder {void setBidderId(Long bidderId);void addItem(Long itemId);void setShippingInfo(ShippingInfo shippingInfo);void setBillingInfo(BillingInfo billingInfo);Long confirmOrder();}
package com.ejb3inaction.example.buslogic;import javax.ejb.EJB;public class PlaceOrderClient {@EJB//注入EJB的实例private static PlaceOrder placeOrder;public static void main(String[] args) {try {Context context = new InitialContext();placeOrder = (PlaceOrder) context.lookup("chapter2/"+ PlaceOrderBean.class.getSimpleName() + "/remote");System.out.println("Exercising PlaceOrder EJB...");placeOrder.setBidderId(new Long(100));placeOrder.addItem(new Long(200));placeOrder.addItem(new Long(201));placeOrder.setShippingInfo(new ShippingInfo("123 My Sweet Home","My City", "My State"));placeOrder.setBillingInfo(new BillingInfo("VISA", "123456789","0708"));Long orderId = placeOrder.confirmOrder();System.out.println("Added Order: " + orderId);} catch (Exception ex) {ex.printStackTrace();}}}
1564


&spm=1001.2101.3001.11974&articleId=51873789&d=1&t=3&u=a890ad1768834f7584991172d2c5b1a4)

被折叠的 条评论
为什么被折叠?



