New features in EJB3.1 (Part 4)

New features in EJB3.1 (Part 4),第1张

 作者:Reza Rahman  文章来源:www.theserverside.com

In the first three articles of this series, I covered optional interfaces for Session beans, Singleton beans, EJB Timer Service enhancements, simplified packaging, asynchronous Session bean invocation and EJB Lite. By popular demand, in this article I will cover WebBeans/EJB 3.1 integration. Remember, none of this has been finalized yet. All of this is really just a peek into the inner workings of the JCP so that you have a chance to provide feedback.

WebBeans and EJB: Together Forever

WebBeans is one of the most exciting JSRs being developed in the Java EE 6 timeframe. The basic value proposition of WebBeans is actually pretty intuitive. If you have developed an application or two using JSF and EJB 3.0, you may have noticed that the JSF backing bean layer is usually pretty thin. In fact, even though you can easily inject EJBs into backing beans using the @EJB annotation, it’s not too far off to classify backing beans as glue-code. WebBeans eliminates this glue-code by allowing you to directly use EJBs as JSF backing beans. We’ll see how great this actually looks in a second.

In addition to effectively integrating the JSF and EJB programming models, WebBeans also adds a number of very cool features including robust annotation-driven DI, further ease-of-use for using the Java EE interceptor model as well as sensible component context management for web applications. This last feature I won’t talk about in great detail since it is requires more of a JSF focus than an EJB focus, but I’ll cover the rest.

WebBeans is largely inspired by JBoss Seam as well as Google Guice. The JSR is being led by Gavin King and Bob Lee is part of the expert group. My guess is that the core Seam code is going to be the basis for the major WebBeans pluggable implementation although it looks like many application servers like Caucho Resin will provide their own implementation as well.

EJBs as JSF Backing Beans

Let’s see what WebBeans is all about by re-factoring an example from EJB 3 in Action. The first substantial Session bean example in EJB 3 in Action is used for adding a bid. The session bean uses JPA to save a Bid entity into the database. Here is how the session bean and the entity would look like in a WebBeans environment:

  1. @Component  
  2. @Stateless  
  3. @Named("placeBid")  
  4. public class PlaceBidBean {  
  5.     @PersistenceContext  
  6.     private EntityManager entityManager;  
  7.       @In  
  8.       private Bid bid;  
  9.     public void addBid() {  
  10.         entityManager.persist(bid);  
  11.     }  
  12. }  
  13. @Component  
  14. @Entity  
  15. @Named("bid")  
  16. public class Bid {  
  17.     private Long bidId;  
  18.     private String bidder;  
  19.     private String item;  
  20.     private Double bidPrice;  
  21.     @Id  
  22.     @GeneratedValue  
  23.     public Long getBidId() {  
  24.         return bidId;  
  25.     }  
  26.     public void setBidId(Long bidId) {  
  27.         this.bidId = bidId;  
  28.     }  
  29.     public String getBidder() {  
  30.         return bidder;  
  31.     }  
  32.     public void setBidder(String bidder) {  
  33.         this.bidder = bidder;  
  34.     }  
  35.     public String getItem() {  
  36.         return item;  
  37.     }  
  38.     public void setItem(String item) {  
  39.         this.item = item;  
  40.     }  
  41.     public Double getBidPrice() {  
  42.         return bidPrice;  
  43.     }  
  44.     public void setBidPrice(Double bidPrice) {  
  45.         this.bidPrice = bidPrice;  
  46.     }  
  47. }  

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/2085435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存