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

  • Injecting Sring Bean in EJB 3.0 Stateless Bean WAS 7.0

    Hello,
    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
    simpleJdbcTemplate.

    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:

    Code:
    @PostConstruct
    	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 org.springframework.context.support.ClassPathXmlAp 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

    STACK TRACE :

    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 ?

    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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- This context is loaded by type and not by name -->
    	<bean id="contextFromBeanRef.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    		<constructor-arg value="classpath*:app-servlet.xml" />
    	</bean>
    </beans>

  • #2
    solved

    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.

    Marius

    Comment

    Working...
    X