Announcement Announcement Module
Collapse
No announcement yet.
Use Spring from JBOSS Service (SAR)? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use Spring from JBOSS Service (SAR)?

    Hi,

    I need to access one of my EJBs from a JBoss SAR file that is within the EAR file that contains my EJB. Anyone know how to load spring from a jboss-service.xml file?

  • #2
    Here's how I do it, with my sar MBean, and in my web app I use a custom DespatcherServlet to load the same context aa a parent context for the web app. See also the javadoc for SingletonBeanFactoryLocator.

    Hope this helps:


    ---------------- my sar MBean ------------------
    Code:
    public class OrderUnload  extends org.jboss.system.ServiceMBeanSupport implements OrderUnloadMBean {
        BeanFactoryReference bfr = null;
        OrderUnloader unloader;
    
        public void startService() {
            BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
            bfr = bfl.useBeanFactory("org.stl.wo.appcontext");
            setUnloader((OrderUnloader)bfr.getFactory().getBean("orderUnloader"));
        }
    
        public void unloadOrders() {
            try {
                unloader.unloadOrders();
            } catch (Exception e) {
                System.out.println("Download of Orders failed:");
                e.printStackTrace();
            }
        }
    
        public void stopService() {
            if (bfr != null) {
                bfr.release();
            }
        }
    
        public void setUnloader(OrderUnloader unloader) {
            this.unloader = unloader;
        }
    }
    -------------------- my beanRefFactory.xml ---------
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans
      PUBLIC "-//SPRING//DTD BEAN//EN"
      "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
        <bean id="org.stl.wo.appcontext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
            <constructor-arg>
                <list>
                    <value>wo_ref_config.xml</value>
                    <value>wo_scheduler_config.xml</value>
                    <value>wo_bus_config.xml</value>
                </list>
            </constructor-arg>
        </bean>
    </beans>
    ----------- my CustomContextDispatcherServlet ------------
    Code:
    public class  CustomContextDispatcherServlet extends DispatcherServlet &#123;
        BeanFactoryReference bfr = null;
    
        protected WebApplicationContext initWebApplicationContext&#40;&#41; &#123;
            ContextLoader cl = new MyContextLoader&#40;&#41;;
            WebApplicationContext wac = cl.initWebApplicationContext&#40;this.getServletContext&#40;&#41;&#41;;
            getServletContext&#40;&#41;.setAttribute&#40;"org.springframework.web.servlet.FrameworkServlet.CONTEXT.woapp", wac&#41;;
            return wac;
        &#125;
    
        public void destroy&#40;&#41; &#123;
            // to prevent classloader problems on hot redeploy&#58;
            if &#40;bfr != null&#41; &#123;
                bfr.release&#40;&#41;;
            &#125;
        &#125;
    
        class MyContextLoader extends ContextLoader &#123;
            protected ApplicationContext loadParentContext&#40;ServletContext servletContext&#41; &#123;
                //System.out.println&#40;"overriding loadParentContext"&#41;;
                BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance&#40;&#41;;
                bfr = bfl.useBeanFactory&#40;"org.stl.wo.appcontext"&#41;;
                return &#40;ApplicationContext&#41;bfr.getFactory&#40;&#41;;
            &#125;
        &#125;
    &#125;

    Comment

    Working...
    X