Announcement Announcement Module
Collapse
No announcement yet.
Autowiring in EJB3 SLSB on JBoss5 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring in EJB3 SLSB on JBoss5

    Hi everyone,

    I have a problem with autowiring Spring beans into EJB3. I've read most articles about it (including, of course, Spring documentation), so I know to use SpringBeanAutowiringInterceptor. I believe I have proper configuration as people were saying was OK, for example here: http://forum.springsource.org/showthread.php?t=49267. But everyone uses JBoss 4.2, I'm using 5.0.1.GA, and I think that it makes some difference.

    Here's my configuration and code:
    EJB:
    Code:
    package foo.ejb;
    
    import javax.ejb.Stateless;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import foo.dao.SthDAO;
    import foo.vo.SthVO;
    
    @Stateless(name = "foo/SessionService", mappedName = "foo/SessionService")
    public class SessionServiceEJB implements SessionService {
    
    	private static final Logger logger = Logger.getLogger(SessionServiceEJB.class);
    
    	@Autowired
    	private SthDAO sthDAO;
    
    	@Override
    	public SthVO getSthById(Long id) {
    		logger.debug("getSthById(" + id + "), sthDAO: " + sthDAO);
    		return sthDAO.getSthById(id);
    	}
    }
    ejb-jar.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
    	<interceptors>
    		<interceptor>
    			<interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
    		</interceptor>
    	</interceptors>
    	<assembly-descriptor>
    		<interceptor-binding>
    			<ejb-name>*</ejb-name>
    			<interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
    		</interceptor-binding>
    	</assembly-descriptor>
    </ejb-jar>
    beanRefContext.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
    
    	<bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
    		<constructor-arg>
    			<list>
    				<value>applicationContext.xml</value>
    			</list>
    		</constructor-arg>
    	</bean>
    </beans>
    applicationContext.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
    	default-autowire="byType">
    	<context:component-scan base-package="foo" />
    	<jee:jndi-lookup id="fooEntityManagerFactory" jndi-name="persistence-units/foo"/>
    </beans>
    I see, that Spring is being used, because in logs I can see:
    Code:
    INFO  [NamedXmlApplicationContext] Bean factory for application context [org.jboss.spring.factory.NamedXmlApplicationContext@1e0a078]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1c56526
    But any try to use EJB3 from Tomcat ends with (Tomcat side):
    Code:
    Caused by: org.springframework.beans.factory.access.BootstrapException: Unable to return specified BeanFactory instance: factory key [null], from group with resource name [classpath*:beanRefContext.xml]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 0
    Has anyone managed to autowire anything into EJB3 on JBoss5? I need that in EAR archive.

    Regards

    Jacek Bilski

  • #2
    The same here but Jboss 5.1.0 and auto-wiring simple pojos.

    Mainly it seems to happen when an persisted ejbTimer method (@Timeout) is invoked of the SLSB and it is fired directly after deployment

    10:43:42,817 INFO [ClassPathXmlApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@2fb3d0: display name [org.springframework.context.support.ClassPathXmlAp plicationContext@2fb3d0]; startup date [Thu Aug 20 10:43:42 CEST 2009]; root of context hierarchy
    10:43:42,918 INFO [ClassPathXmlApplicationContext] Bean factory for application context [org.springframework.context.support.ClassPathXmlAp plicationContext@2fb3d0]: org.springframework.beans.factory.support.DefaultL istableBeanFactory@8da839
    10:43:42,932 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@8da839: defining beans []; root of factory hierarchy
    10:43:42,952 ERROR [TimerImpl] Error invoking ejbTimeout
    javax.ejb.EJBException: java.lang.RuntimeException: org.springframework.beans.factory.access.Bootstrap Exception: Unable to return specified BeanFactory instance: factory key [null], from group with resource name [classpath*:beanRefContext.xml]; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 0
    at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOu rTx(Ejb3TxPolicy.java:77)
    at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPoli cy.java:83)
    at org.jboss.aspects.tx.TxInterceptor$Required.invoke (TxInterceptor.java:190)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.aspects.tx.TxPropagationInterceptor.invo ke(TxPropagationInterceptor.java:76)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInter ceptor.java:42)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.ejb3.security.RoleBasedAuthorizationInte rceptorv2.invoke(RoleBasedAuthorizationInterceptor v2.java:138)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.ejb3.security.Ejb3AuthenticationIntercep torv2.invoke(Ejb3AuthenticationInterceptorv2.java: 80)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.ejb3.ENCPropagationInterceptor.invoke(EN CPropagationInterceptor.java:41)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.ejb3.BlockContainerShutdownInterceptor.i nvoke(BlockContainerShutdownInterceptor.java:67)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.aspects.currentinvocation.CurrentInvocat ionInterceptor.invoke(CurrentInvocationInterceptor .java:67)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.ejb3.stateless.StatelessContainer.callTi meout(StatelessContainer.java:249)
    at org.jboss.as.ejb3.timerservice.TimedObjectInvokerB ridge.callTimeout(TimedObjectInvokerBridge.java:44 )
    at org.jboss.ejb.txtimer.TimerImpl$TimerTaskImpl.run( TimerImpl.java:561)
    at java.util.TimerThread.mainLoop(Timer.java:512)
    at java.util.TimerThread.run(Timer.java:462)
    Caused by: java.lang.RuntimeException: org.springframework.beans.factory.access.Bootstrap Exception: Unable to return specified BeanFactory instance: factory key [null], from group with resource name [classpath*:beanRefContext.xml]; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 0
    at org.jboss.ejb3.EJBContainer.invokeCallback(EJBCont ainer.java:1123)
    at org.jboss.ejb3.EJBContainer.invokePostConstruct(EJ BContainer.java:1133)
    at org.jboss.ejb3.EJBContainer.invokePostConstruct(EJ BContainer.java:1144)
    at org.jboss.ejb3.pool.AbstractPool.create(AbstractPo ol.java:93)
    at org.jboss.ejb3.InfinitePool.get(InfinitePool.java: 56)
    at org.jboss.ejb3.InfinitePool.get(InfinitePool.java: 51)
    at org.jboss.ejb3.pool.ThreadlocalPool.create(Threadl ocalPool.java:53)
    at org.jboss.ejb3.pool.ThreadlocalPool.get(Threadloca lPool.java:93)
    at org.jboss.ejb3.stateless.StatelessInstanceIntercep tor.invoke(StatelessInstanceInterceptor.java:58)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNex t(MethodInvocation.java:102)
    at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPoli cy.java:79)
    ... 21 more
    Caused by: org.springframework.beans.factory.access.Bootstrap Exception: Unable to return specified BeanFactory instance: factory key [null], from group with resource name [classpath*:beanRefContext.xml]; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 0
    at org.springframework.beans.factory.access.Singleton BeanFactoryLocator.useBeanFactory(SingletonBeanFac toryLocator.java:410)
    at org.springframework.ejb.interceptor.SpringBeanAuto wiringInterceptor.getBeanFactoryReference(SpringBe anAutowiringInterceptor.java:147)
    at org.springframework.ejb.interceptor.SpringBeanAuto wiringInterceptor.getBeanFactory(SpringBeanAutowir ingInterceptor.java:128)
    at org.springframework.ejb.interceptor.SpringBeanAuto wiringInterceptor.doAutowireBean(SpringBeanAutowir ingInterceptor.java:108)
    at org.springframework.ejb.interceptor.SpringBeanAuto wiringInterceptor.autowireBean(SpringBeanAutowirin gInterceptor.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.ejb3.interceptors.aop.LifecycleCallbackI nterceptorMethodInterceptor.invoke(LifecycleCallba ckInterceptorMethodInterceptor.java:80)
    at org.jboss.aop.joinpoint.ConstructionInvocation.inv okeNext(ConstructionInvocation.java:79)
    at org.jboss.ejb3.interceptors.aop.InvocationContextI nterceptor.setup(InvocationContextInterceptor.java :88)
    at org.jboss.aop.advice.org.jboss.ejb3.interceptors.a op.InvocationContextInterceptor_z_setup_4373267.in voke(InvocationContextInterceptor_z_setup_4373267. java)
    at org.jboss.aop.joinpoint.ConstructionInvocation.inv okeNext(ConstructionInvocation.java:79)
    at org.jboss.aspects.currentinvocation.CurrentInvocat ionInterceptor.invoke(CurrentInvocationInterceptor .java:67)
    at org.jboss.aop.joinpoint.ConstructionInvocation.inv okeNext(ConstructionInvocation.java:79)
    at org.jboss.ejb3.EJBContainer.invokeCallback(EJBCont ainer.java:1119)
    ... 31 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 0
    at org.springframework.beans.factory.BeanFactoryUtils .beanOfType(BeanFactoryUtils.java:379)
    at org.springframework.beans.factory.access.Singleton BeanFactoryLocator.useBeanFactory(SingletonBeanFac toryLocator.java:400)
    ... 47 more
    Seems like a classloading issue to me - not finding the classpath*:beanRefContext.xml. Without the @Timeout annotation it seems to work.

    Any pointers?
    Last edited by vguna; Aug 20th, 2009, 04:00 AM.

    Comment

    Working...
    X