作者: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 ForeverWebBeans 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 BeansLet’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:
- @Component
- @Stateless
- @Named("placeBid")
- public class PlaceBidBean {
- @PersistenceContext
- private EntityManager entityManager;
- @In
- private Bid bid;
- public void addBid() {
- entityManager.persist(bid);
- }
- }
- @Component
- @Entity
- @Named("bid")
- public class Bid {
- private Long bidId;
- private String bidder;
- private String item;
- private Double bidPrice;
- @Id
- @GeneratedValue
- public Long getBidId() {
- return bidId;
- }
- public void setBidId(Long bidId) {
- this.bidId = bidId;
- }
- public String getBidder() {
- return bidder;
- }
- public void setBidder(String bidder) {
- this.bidder = bidder;
- }
- public String getItem() {
- return item;
- }
- public void setItem(String item) {
- this.item = item;
- }
- public Double getBidPrice() {
- return bidPrice;
- }
- public void setBidPrice(Double bidPrice) {
- this.bidPrice = bidPrice;
- }
- }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)