Announcement Announcement Module
Collapse
No announcement yet.
EJB 3.0 with Spring 2.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • EJB 3.0 with Spring 2.0

    Can i use ejb 3.0 with spring 1.2 version?

    Thanks.
    Last edited by prajkta; Mar 26th, 2008, 03:50 PM.

  • #2
    Do you mean on the client or server?

    Client side is simple. The EJB3 object is just a JNDI object which implements a particular interface. So you can use JndiObjectFactory to look it up.

    On the server side, you can mix EJB3 and Spring. Since EJB3 objects are created by the container and not Spring, I'm not sure if you could use any of the AspectJ Spring functionality (like @Configurable/@Autowire) on an EJB3 object. You'd have to try it (if that is something you need).

    Comment


    • #3
      Thanks Bill. i think you have very good knowledge of ejbs. do u know any good ,detailed tutorial related to spring EJB?

      thanks.
      Last edited by prajkta; Mar 27th, 2008, 09:20 AM.

      Comment


      • #4
        Bill,
        sorry i needed to be more precise
        I have an EJB3 in which i want to inject a spring bean..
        i have followed this link

        http://twasink.net/blog/archives/200...spring_wi.html

        and down the article it mentioned

        SpringBeanAutowiringInterceptor

        here's my code
        Code:
        @Stateless (name="Facade")
        @Interceptors(SpringBeanAutowiringInterceptor.class)
        public class StatelessFacade implements Facade {
        
        
        	@Autowired
        	private MySpringBean mySpringBean;
        
        	public String getFoobar() {
        		return "Foobar";
        	}
        }
        here'sm by beanRefContext.xml
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
        
        <beans>
        
        
        	<bean id="factoryKey"
        		class="org.springframework.context.support.ClassPathXmlAp plicationContext">
        		<constructor-arg value="myBeansContext.xml"/>
        	</bean>
        </beans>
        and here' smy myBeansContext.xml
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
        
        <!--
          - Application context for EJB3
          -->
        
        <beans>
        
        	<bean id="simpleBean"
        		class="com.mm.j2me.spring.MySpringBean"/>
        	
        	</bean>
        </beans>
        and here' sexception
        Code:
        java.lang.RuntimeException: org.springframework.beans.factory.access.BootstrapEx
        ception: Unable to initialize group definition. Group resource name [classpath*:
        beanRefContext.xml], factory key [null]; nested exception is org.springframework
        .beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframew
        ork.context.support.ClassPathXmlAp plicationContext] for bean with name 'factory
        Key' defined in URL [file:/C:/Sw/J2MEJobApp/ejbJ2ME/target/classes/beanRefContex
        t.xml]; nested exception is java.lang.ClassNotFoundException: org.springframewor
        k.context.support.ClassPathXmlAp plicationContext
                at org.jboss.ejb3.interceptor.LifecycleInterceptorHandler.postConstruct(
        LifecycleInterceptorHandler.java:113)
                at org.jboss.ejb3.EJBContainer.invokePostConstruct(EJBContainer.java:505
        )
                at org.jboss.ejb3.AbstractPool.create(AbstractPool.java:112)
                at org.jboss.ejb3.ThreadlocalPool.get(ThreadlocalPool.java:48)
                at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(Stateles
        sInstanceInterceptor.java:54)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
        java:101)
                at org.jboss.aspects.security.AuthenticationInterceptor.invoke(Authentic
        ationInterceptor.java:77)
                at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3Auth
        enticationInterceptor.java:102)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
        java:101)
                at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterce
        ptor.java:47)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
        java:101)
                at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(Asynchrono
        usInterceptor.java:106)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
        java:101)
                at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessCont
        ainer.java:211)
                at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalPro
        xy.java:79)
                at $Proxy28.getFoobar(Unknown Source)
                at example1.StatelessFacadeTest.testFoobar(StatelessFacadeTest.java:38)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
        java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
        sorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:552)
                at org.testng.internal.Invoker.invokeMethod(Invoker.java:411)
                at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:785)
                at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:114)
                at org.testng.TestRunner.privateRun(TestRunner.java:693)
                at org.testng.TestRunner.run(TestRunner.java:574)
                at org.testng.SuiteRunner.privateRun(SuiteRunner.java:241)
                at org.testng.SuiteRunner.run(SuiteRunner.java:145)
                at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:901)
                at org.testng.TestNG.runSuitesLocally(TestNG.java:863)
                at org.testng.TestNG.run(TestNG.java:613)
                at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.ja
        va:74)
                at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXml
        TestSuite.java:92)
                at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
        java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
        sorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(Su
        refireBooter.java:339)
                at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.j
        ava:997)
        Caused by: org.springframework.beans.factory.access.BootstrapException: Unable t
        o initialize group definition. Group resource name [classpath*:beanRefContext.xm
        l], factory key [null]; nested exception is org.springframework.beans.factory.Ca
        nnotLoadBeanClassException: Cannot find class [org.springframework.context.suppo
        rt.ClassPathXmlAp plicationContext] for bean with name 'factoryKey' defined in U
        RL [file:/C:/Sw/J2MEJobApp/ejbJ2ME/target/classes/beanRefContext.xml]; nested ex
        ception is java.lang.ClassNotFoundException: org.springframework.context.support
        .ClassPathXmlAp plicationContext
                at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.
        useBeanFactory(SingletonBeanFactoryLocator.java:389)
                at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.g
        etBeanFactoryReference(SpringBeanAutowiringInterceptor.java:132)
                at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.g
        etBeanFactory(SpringBeanAutowiringInterceptor.java:113)
                at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.a
        utowireBean(SpringBeanAutowiringInterceptor.java:93)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
        java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
        sorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.jboss.ejb3.interceptor.LifecycleInvocationContextImpl.proceed(Lif
        ecycleInvocationContextImpl.java:131)
                at org.jboss.ejb3.interceptor.LifecycleInterceptorHandler.postConstruct(
        LifecycleInterceptorHandler.java:109)
                ... 40 more
        Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Canno
        t find class [org.springframework.context.support.ClassPathXmlAp plicationContex
        t] for bean with name 'factoryKey' defined in URL [file:/C:/Sw/J2MEJobApp/ejbJ2M
        E/target/classes/beanRefContext.xml]; nested exception is java.lang.ClassNotFoun
        dException: org.springframework.context.support.ClassPathXmlAp plicationContext
                at org.springframework.beans.factory.support.AbstractBeanFactory.resolve
        BeanClass(AbstractBeanFactory.java:1173)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBean
        Factory.predictBeanType(AbstractAutowireCapableBeanFactory.java:479)
                at org.springframework.beans.factory.support.AbstractBeanFactory.isFacto
        ryBean(AbstractBeanFactory.java:787)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.
        preInstantiateSingletons(DefaultListableBeanFactory.java:393)
                at org.springframework.context.support.AbstractApplicationContext.finish
        BeanFactoryInitialization(AbstractApplicationContext.java:736)
                at org.springframework.context.support.AbstractApplicationContext.refres
        h(AbstractApplicationContext.java:369)
                at org.springframework.context.access.ContextSingletonBeanFactoryLocator
        .initializeDefinition(ContextSingletonBeanFactoryLocator.java:141)
                at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.
        useBeanFactory(SingletonBeanFactoryLocator.java:384)
                ... 49 more
        Caused by: java.lang.ClassNotFoundException: org.springframework.context.support
        .ClassPathXmlAp plicationContext
                at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                at org.springframework.util.ClassUtils.forName(ClassUtils.java:230)
                at org.springframework.beans.factory.support.AbstractBeanDefinition.reso
        lveBeanClass(AbstractBeanDefinition.java:381)
                at org.springframework.beans.factory.support.AbstractBeanFactory.resolve
        BeanClass(AbstractBeanFactory.java:1170)
                ... 56 more
        i m running my tests using jboss embedded container and testNG, and when i ran tehm using mvn -X to see what's in classpath i can clearly see spring-2.5.1.jar
        Code:
        C:\Documents and Settings\Marco Mistroni\.m2\repository\org\springframework\spring\2.5.1\spring-2.5.1.jar, C:\Sw\J2MEJobApp\ejbJ2ME\lib\jbossws-client-1.3.jar]

        could you please help me out?

        thanks and regards
        marco




        thanks and regards
        marco

        Comment


        • #5
          Hello,
          think i might have found problem - as per resulto fo a google search -
          i am using Spring 2.5, and as well ejb embeddable container which has cglib classes in his thirdparty-all.jar jar file
          problem is taht i don't know which version of ehcache is using...

          might that be the reason?

          regards
          marco

          Comment


          • #6
            Hello,
            ok after some debugging i found the problem: I M AN IDIOT

            this was the problem, an incorrect declaration of ClassPathXmlApplicationContext
            Code:
            <bean id="factoryKey"
            		class="org.springframework.context.support.ClassPathXmlAp plicationContext">
            		<constructor-arg value="myBeansContext.xml"/>
            	</bean>
            i left a space between ClassPathXmlAp and plicationContext

            everything works as a charm... ejb3 + embedded container + spring2.5 + testng + mvn2......

            sorry for messing up... i definitely should not code on saturday evening.....

            rgds
            marco

            Comment


            • #7
              Hi,

              I have beanRefContext.xml which has a reference to applicationContext.xml where in the bean declarations are given.
              I am using the @Autowired annotation for injecting the bean instance to the instance variable declared in EJB.

              beanRefContext.xml
              <beans>
              <bean id="businessBeanFactory"
              class="org.springframework.context.support.ClassPa thXmlApplicationContext">
              <constructor-arg value="/applicationContext.xml" />
              </bean>
              </beans>


              applicationContext.xml

              [COLOR="rgb(105, 105, 105)"]<bean id="searchEngine"
              class="com.engine.SearchEngine"
              autowire="byName"/>
              </beans>[/COLOR]


              MyBean.java

              [COLOR="rgb(105, 105, 105)"]@Session(ejbName = "MyBean")
              @JndiName(remote = "ejb. MyBeanRemoteHome")
              @FileGeneration(remoteClass = Constants.Bool.TRUE, remoteHome = Constants.Bool.TRUE, localClass = Constants.Bool.FALSE, localHome = Constants.Bool.FALSE)
              @Interceptors(SpringBeanAutowiringInterceptor.clas s)
              public class MyBean extends GenericSessionBean implements SessionBean {
              private static final long serialVersionUID = 1L;

              @Autowired
              private ISearchEngine searchEngine;

              public void testEngine(){
              searchEngine.searchMethod();
              }
              }[/COLOR]

              I am getting the NullPointerException on executing the above program. Thing is, the object is not populated into the instance variable searchEngine. What i doubt is that beanRefContext.xml is not being considered by the Weblogic.

              FYI, i am using Weblogic 10.2 and IDE is Weblogic studio.

              Please, help me in this.

              Comment

              Working...
              X