4 Replies Latest reply on Jul 27, 2009 11:05 AM by Martin Lynch

    Cant connect to EJB using from applicationContext.xml

    Martin Lynch Newbie

      Hi

      Just got my first bean deployed in JBoss but Im having difficulty trying to talk to it from one of my java test classes.... Here's what Im trying to do....

      Oh I should point out im new to all this stuff so apologies if its really simple...

      try {
       ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
      
       service = (TokenManagementService) ctx
       .getBean("tokenManagementService");
      
       } catch (BeansException e) {
       e.printStackTrace();
       throw new BusinessLogicException(e.getMessage());
       }



      My applicationContext.xml looks like this....
      <bean id="token" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
      
       <property name="jndiName">
       <value>ejb/tokenManagementService</value>
       </property>
      
       <property name="resourceRef">
       <value>true</value>
       </property>
      
       <property name="businessInterface">
       <value>
       aero.sita.gsl.bio.services.backEnd.tokenManagement.ITokenManagement
       </value>
       </property>
      
       </bean>




      And this is the error that's spewing out....
      27-Jul-2009 14:35:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
      INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@16f8cd0: display name [org.springframework.context.support.ClassPathXmlApplicationContext@16f8cd0]; startup date [Mon Jul 27 14:35:52 BST 2009]; root of context hierarchy
      27-Jul-2009 14:35:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
      INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
      27-Jul-2009 14:35:53 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
      INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@16f8cd0]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1125127
      27-Jul-2009 14:35:53 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
      INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1125127: defining beans [token]; root of factory hierarchy
      27-Jul-2009 14:35:53 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
      INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1125127: defining beans [token]; root of factory hierarchy
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'token' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
       at java.security.AccessController.doPrivileged(Native Method)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
       at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
       at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
       at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
       at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
       at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
       at aero.sita.gsl.bio.services.frontEnd.businessLogic.step.verification.ValidateTokenStep.processRequestImpl(ValidateTokenStep.java:39)
       at aero.sita.gsl.bio.services.frontEnd.businessLogic.step.verification.TestValidateTokenStep.testProcessRequestImpl(TestValidateTokenStep.java:41)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at junit.framework.TestCase.runTest(TestCase.java:168)
       at junit.framework.TestCase.runBare(TestCase.java:134)
       at junit.framework.TestResult$1.protect(TestResult.java:110)
       at junit.framework.TestResult.runProtected(TestResult.java:128)
       at junit.framework.TestResult.run(TestResult.java:113)
       at junit.framework.TestCase.run(TestCase.java:124)
       at junit.framework.TestSuite.runTest(TestSuite.java:232)
       at junit.framework.TestSuite.run(TestSuite.java:227)
       at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
       at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
       at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
       at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
       at javax.naming.InitialContext.lookup(Unknown Source)
       at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
       at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88)
       at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153)
       at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
       at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104)
       at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
       at org.springframework.ejb.access.AbstractSlsbInvokerInterceptor.refreshHome(AbstractSlsbInvokerInterceptor.java:121)
       at org.springframework.ejb.access.AbstractSlsbInvokerInterceptor.afterPropertiesSet(AbstractSlsbInvokerInterceptor.java:108)
       at org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean.afterPropertiesSet(LocalStatelessSessionProxyFactoryBean.java:85)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
       ... 35 more
      aero.sita.gsl.bio.services.frontEnd.commonCore.exception.BusinessLogicException: Error creating bean with name 'token' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
       at aero.sita.gsl.bio.services.frontEnd.businessLogic.step.verification.ValidateTokenStep.processRequestImpl(ValidateTokenStep.java:42)
       at aero.sita.gsl.bio.services.frontEnd.businessLogic.step.verification.TestValidateTokenStep.testProcessRequestImpl(TestValidateTokenStep.java:41)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at junit.framework.TestCase.runTest(TestCase.java:168)
       at junit.framework.TestCase.runBare(TestCase.java:134)
       at junit.framework.TestResult$1.protect(TestResult.java:110)
       at junit.framework.TestResult.runProtected(TestResult.java:128)
       at junit.framework.TestResult.run(TestResult.java:113)
       at junit.framework.TestCase.run(TestCase.java:124)
       at junit.framework.TestSuite.runTest(TestSuite.java:232)
       at junit.framework.TestSuite.run(TestSuite.java:227)
       at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


      All help is appreciated....

      thanks
      marty

        • 1. Re: Cant connect to EJB using from applicationContext.xml
          Muhammed Patel Newbie

          Hey marty,

          I am as new to ejbs as you are but i do know spring a little.
          Just had a look at your code an noticed that you are calling a bean that does not exist.

          so

          service = (TokenManagementService) ctx
           .getBean("tokenManagementService");
          

          should probably be
          service = (TokenManagementService) ctx
           .getBean("token");
          


          Don't know if i am right. but i hope that helps :D if anything.


          • 2. Re: Cant connect to EJB using from applicationContext.xml
            Martin Lynch Newbie

            Hi

            Thanks for the suggestion... tried it but no luck....

            here are my ejb-jar and jboss XML files in case they might help show something....

            <?xml version = '1.0' encoding = 'UTF-8'?>
            <ejb-jar version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_3_0.xsd">
             <enterprise-beans>
             <session>
             <description>TokenManagementService Bean</description>
             <ejb-name>TokenManagementServiceEJB</ejb-name>
             <mapped-name>ejb/tokenManagementService</mapped-name>
             <local-home>aero.sita.gsl.bio.services.backEnd.tokenManagement.TokenManagementServiceHome</local-home>
             <local>aero.sita.gsl.bio.services.backEnd.tokenManagement.TokenManagementServiceLocal</local>
             <ejb-class>aero.sita.gsl.bio.services.backEnd.tokenManagement.TokenManagementServiceEJB</ejb-class>
             <session-type>Stateless</session-type>
             <transaction-type>Container</transaction-type>
             <env-entry>
             <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
             <env-entry-type>java.lang.String</env-entry-type>
             <env-entry-value>applicationContext.xml</env-entry-value>
             </env-entry>
             </session>
             </enterprise-beans>
            </ejb-jar>
            


            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_5_0.dtd">
            
            <jboss>
             <enterprise-beans>
             <session>
             <ejb-name>TokenManagementServiceEJB</ejb-name>
             <jndi-name>ejb/tokenManagementService</jndi-name>
             </session>
             </enterprise-beans>
            </jboss>
            


            marty....

            • 3. Re: Cant connect to EJB using from applicationContext.xml
              Marius Bogoevici Expert

              The bean name being wrong was a separate issue, but you also have a JNDI configuration problem.

              In your case, you need to set the jndiEnvironment property on your LocalStatelessSessionProxyFactoryBean, which is essentially a Properties object that you need to set up for being able to look up your JNDI InitialContext.

              • 4. Re: Cant connect to EJB using from applicationContext.xml
                Martin Lynch Newbie

                Hi Marius

                Im sorry but i dont understand what you mean... as I said, im a beginner at this stuff...

                could you please provide an example?

                thanks
                marty