Announcement Announcement Module
No announcement yet.
Injecting Sring Bean in EJB 3.0 Stateless Bean WAS 7.0 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting Sring Bean in EJB 3.0 Stateless Bean WAS 7.0

    I am using EJB 3.0 on WAS and Spring MVC.
    I have maneged to inject a Stateless bean into a @Controller spring bean.
    I want to inject some spring beans in a stateless bean. One is the ldapTemplate and the other one is

    I have found SpringBeanAutowiringInterceptor.class and configured beanRefContext.xml.
    I am newbie in Spring, and it took me a while . Using this method did not work so I believed it was a websphere
    probleme, and I used the following to retrieve my bean:

    	protected void afterInit() {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:app-servlet.xml");
    		ldapTemplate = (LdapTemplate)context.getBean("ldapTemplate"); 
    		log.debug("simpleJdbcTemplate bean inserted");
    The error I got was :No bean named 'ldapTemplate' is defined so I remembered that
    the error with using the method with the interceptor was No bean named 'ldapTemplate' is defined ...
    and realized something else is wrong, I think I am loosing my beans ...

    I do not know how Spring instantiates the beans(I thought it was doing this only at the start of the application):
    a) When starting the application I see in my log :
    ... loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/app-servlet.xml]
    DefaultListableBeanFactory@5eeb5eeb: defining beans defining beans [org.springframework.context.annotation.internalCon figurationAnnotationProcessor, ...ldapTemplate, ...

    b) After entering the ejb method :
    ...prepareRefresh Refreshing plicationContext
    ...loadBeanDefinitions Loading XML bean definitions from URL [file:/D:/xxx/EJB_30_Framework/ejbModule/beanRefContext.xml]
    ...DefaultListableBeanFactory@62456245: defining beans []; root of factory hierarchy


    EJB threw an unexpected (non-declared) exception during invocation of method "searchADUser". Exception data: javax.ejb.EJBException: session bean post construct failure; nested exception is: org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'ldapTemplate' is defined

    So what happends after Spring parses the beanRefContext.xml file ?
    My beans are declared in the file app-servlet.xml, should it not keep my current app context ?

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	xmlns:xsi="" xmlns:context=""
    	<!-- This context is loaded by type and not by name -->
    	<bean id="contextFromBeanRef.context" class="">
    		<constructor-arg value="classpath*:app-servlet.xml" />

  • #2

    I solved the problem,

    The app-servlet.xml file must be in the same folder with beanRefContext.xml.
    I've made another xml file, added only the beans that the EJBs are using and it works.