Hot questions for Using Enterprise JavaBeans in spring mvc

Question:

In my project there are two independent modules those are EJB with dao layer and Spring mvc with controller. Beans are there in the commons project, the commons project jar is there in the EJB and MVC projects. There is a bean called user.java, for this user bean i am setting some values in the ejb and i connot able to get in the spring mvc controller.

It is show an Exception

Caused by: java.lang.ClassCastException: com.ex.beans.User cannot be cast to com.ex.beans.User

How can i solve this problem.

appService.java:- in EJB

User user=appDAO.getUser(username);
return user;

In MVC

User user=appService.getUser(username);

Answer:

The mistake i did is i given ejb project dependency in spring mvc pom.xml. In the dependency i given <scope>provided</scoper>. and i changed that into <scope>compile</scoper>, or we can remove that scope from dependency.

Question:

I'm a new Spring. So i've one thing that need to you explain. i read benefits of Spring framework here and i got this:

Spring enables developers to develop enterprise-class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product such as an application server but you have the option of using only a robust servlet container such as Tomcat or some commercial product.

i can't understand why if we use POJO, we do not need an EJB container and just only use a servlet container instead. Someone can make clearly that benefit for me! Thank so much! Sorry about my English, it's not good!


Answer:

POJO – Stands for Plain Old Java Objects. In short plain Objects that meets the java beans requirements. See What is a JavaBean exactly?.

So to use POJOs you just need java (JRE/JDK). If you want Servlet API you add Tomcat etc.

Spring will provide almost all EJB functionality in one way or other but in a more simplified way (and better from my point of view).

For example JPA abstraction using Hibernate or other ORM solution.

As the tutorial say

Spring does not reinvent the wheel instead, it truly makes use of some of the existing technologies like several ORM frameworks, logging frameworks, Java EE, Quartz and JDK timers, other view technologies.

Question:

My EJB is as follows and deployed on Glassfish with JNDI name (retrieved from Glassfish console) - java:global/FinalEJBProject-ejb/AdminOperationsBean!com.humber.its.n01131590.AdminOperationsBeanRemote

@Stateless(mappedName = "ejb/AdminOperationsBean")
public class AdminOperationsBean implements AdminOperationsBeanRemote { }

In my Spring project, I am trying to access the bean. ApplicationContext.xml is as follows - <jee:remote-slsb id="adminRemote" jndi-name="java:global/FinalEJBProject- ejb/AdminOperationsBean!com.humber.its.n01131590.AdminOperationsBeanRemote" business-interface="com.humber.its.n01131590.AdminOperationsBeanRemote" cache-home="false" lookup-home-on-startup="false" refresh-home-on-connect-failure="true" />

In Spring controller -

@Controller
public class AdminController {

private AdminOperationsBeanRemote adminRemote;

public void setAdminOperationsBeanRemote(AdminOperationsBeanRemote 
adminRemote) {
this.adminRemote = adminRemote;
}

public AdminOperationsBeanRemote getAdminOperationsBeanRemote() {
return adminRemote;
}

I am always getting a lookup failed -

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.remoting.RemoteLookupFailureException: Failed to locate remote EJB [java:global/FinalEJBProject-ejb/AdminOperationsBean!com.humber.its.n01131590.AdminOperationsBeanRemote]; nested exception is javax.naming.NamingException: Lookup failed for 'java:global/FinalEJBProject-ejb/AdminOperationsBean!com.humber.its.n01131590.AdminOperationsBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: FinalEJBProject-ejb]

Can someone please help me, I have been on this for the past three days!


Answer:

So I finally figured it out,in your ApplicationContext.xml use the JNDI name without the suffix qualifier name.

java:global/FinalEJBProject-ejb/AdminOperationsBean!com.humber.its.n01131590.AdminOperationsBeanRemote

java:global/FinalEJBProject-ejb/AdminOperationsBean

Use the second JNDI name in the ApplicationContext.xml as follows -

<jee:remote-slsb id="adminRemote" jndi-name="java:global/FinalEJBProject-ejb/AdminOperationsBean" business- interface="com.humber.its.n01131590.AdminOperationsBeanRemote" cache-home="false" lookup-home-on-startup="false" refresh-home-on-connect-failure="true" />