File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Using EJB3 in combination with Spring. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Using EJB3 in combination with Spring." Watch "Using EJB3 in combination with Spring." New topic
Author

Using EJB3 in combination with Spring.

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
I am a query .please advice me :

When using Spring and EJB 3 in a Application .

I had configured DataSources and iBatis XML files inside applicationcontext.xml . --- step1

Inside my JSP page i am directly(means using @EJB ) calling my session bean.

Will this be a good approach if i do so ??

Or

what will the standard approach ??

please advice ,waiting for your ideas.
Thanks in advance


If you want something you never had do something which you had never done
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Hey nobody answering my question .

Lunch time or a holiday and all experts offline ??
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Ravi,

I have presented on this topic, most recently in TSSJS 2009. Here is the presentation and code samples: http://www.ctjava.org/camp2008/sessionB.jsf?cid=2809. Let me know if you still have questions, I can help you. Generally, the best approach is to have all resources like data sources/JMS topics/JMS queues/JavaMail sessions defined in the application server and pull them into either EJB 3 or Spring as needed. I recommend using session beans, message driven beans and JPA from EJB 3 and advanced DI, JDBC template, JMS template, iBATIs and Quartz from Spring on an as-needed basis.

Note, it isn't possible to inject either Spring beans or EJBs into a JSP page. You will need to use look-ups in either case. To use injection, consider Servlets, JSF or any other framework like Struts.

Hope it helps,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Reza,

so nice of you .Thanks for your reply. I will go through the link and ask you doubts on that.Thanks.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Reza , stunned seeing your article .Its excellent .Thank you very much sir.

i am new to iBatis as well as spring .so dont mind if my doubt is a cramp.

Instead of doing all the database operations inside my EJB , can i have a implementation class which gets called by the EJB , and can i mention that implementation class in the applicationcontext.xml file like this :


<bean id="AccountServiceImpl"
class="com.Mypack.service.AccountServiceImpl">


Will there anything advantage i would get by doing so??
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Reza , please tell me this :

How the beanrefContext.xml file gets called upon at application startup time.

Please tell me this.
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Ravi,

RaviNada Kiran wrote:
Will there anything advantage i would get by doing so??


Yes, you can inject any Spring bean into EJB 3 as the code examples show. As I said, if you are using primarily JPA for doing data access, writing the data access layer in Spring does not make much sense (configuration is harder without any practical gain). Writing DAOs in Spring makes more sense if using iBATIS or Spring JDBC. Even then, the DAO layer can be written in EJB 3 as shown in my example code. One very nice thing about having EJB 3 in the data access layer is pooling, server optimizations as well as automatic integration with the app server console for features such as runtime monitoring (not to mention guaranteed transactional context and thread-safety).

Hope it helps,
Reza
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Ravi,

RaviNada Kiran wrote:How the beanrefContext.xml file gets called upon at application start up time.


It is loaded by the Spring bean autowiring interceptor when the EJB container bootstraps. Take a look here: http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.html.

Hope it helps,
Reza

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thank you Reza for your valuable time and nice tutorial.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Hello Reza Rahman ,

I saw your article , it was nice ,but unfortunately i couldn't grab some points on to it.

This line of code on to the servlet



How this piece of above code is able to get access to the AccountService interface which is defined as @Local

It would be great if you make a reply with explanation.
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Pavan,

There is not really a whole lot to explain in the Servlet EJB injection code to be honest. Once you enable OpenEJB on Tomcat, it will handle all Java EE 5 injection, including @EJB: http://openejb.apache.org/tomcat.html.

Let me know if you continue to have questions. The OpenEJB site also has a decent Java EE 5/EJB 3.0 starter tutorial should you need a brief primer.

Hope it helps,
Reza
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Hello Reza.

I had a confusion in understanding a common approach which is typically followed. Could you please make me understand this

We would normally call a DAO interface from our servlet which in in-turn call the DAO implemented class for Data Submission and retrevial. (Correct me if i am wrong)

But where as in a

In a standard EJB3.0 approach why there is no built in DAO layer , where our servlet directly access the EJB with the help of mapped name .
(My question is will the servlet will access the EJB's Remote or Locale interface directly or an EJB implemented class directly ??)

Waiting for your response.


Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Pavan,

I don't think I understand what you are asking me exactly?

Typically, the web tier would invoke the service or business tier. The business tier usually invokes the DAO tier. All these invocations should be happening via interfaces, with the DI system (like EJB 3) resolving interfaces to the correct implementation (for details of how this happens, check out a good EJB 3 reference). If needed, you can specify an EJB 3 name or mapped JNDI name exactly, but it isn't needed in most systems and just @EJB should be sufficient.

What specific DOA support are you looking for? As the code examples demonstrate, you can easily use JPA, iBATIS or Spring JDBC via a DAO tier composed of EJB 3. However, if you want, you cam compose the DAO layer out of Spring beans and simply inject them to the service tier composed of EJB 3.

This make sense?

Cheers,
Reza
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using EJB3 in combination with Spring.