简体   繁体   中英

How to access red5-web.xml beans from spring mvc service

I have a one red5 application using flex. Now I just want to develop a small web application on top on it using spring mvc (DispatcherServlet). But problem is I could not access the beans defined in red5-web.xml from mvc and also I am not able to access red-web beans from spring mvc. The error is NoSuchBeanDefinitionException.

There is no bridge between the two. Is it possible to get the red5.context in spring mvc. so i can access everything. Here is code snip

red5-web.xml

<beans>
    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/red5-web.properties" />
    </bean>

    <bean id="web.context" class="org.red5.server.Context" 
        autowire="byType" />

    <bean id="web.scope" class="org.red5.server.WebScope"
         init-method="register">
        <property name="server" ref="red5.server" />
        <property name="parent" ref="global.scope" />
        <property name="context" ref="web.context" />
        <property name="handler" ref="web.handler" />
        <property name="contextPath" value="${webapp.contextPath}" />
        <property name="virtualHosts" value="${webapp.virtualHosts}" />
    </bean>

    <bean id="live.recorder" class="com.live.application.RecordManager">
        <constructor-arg ref="web.handler" />
    </bean>

    <bean id="web.handler" name="application"
        class="com.live.application.LiveContent"
        singleton="true" autowire="byName" />
</beans>

webapplication-servlet.xml

<beans>

  <context:annotation-config/>
  <context:component-scan base-package="com.live.web"/>
  <context:component-scan base-package="com.live.service"/>
  <mvc:annotation-driven/>

  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/> 

  <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    <property name="order" value="0"/>
  </bean>
  <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
    <property name="order" value="1"/>
  </bean>
  <bean name="users.search_results" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="renderedAttributes" value="users"/>
  </bean>

  <bean name="system.status" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="renderedAttributes">
      <set>
        <value>status</value>
        <value>notifications</value>
      </set>        
    </property>
  </bean>
  <bean name="system.update" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="renderedAttributes">
      <set>
        <value>error</value>
        <value>status</value>
        <value>notification</value>
      </set>        
    </property>
  </bean>

</beans>

I used spring annotation for controller and service.

Finally I fixed this issue. The problem is

Usually spring DispatcherServlet(MVC) creates one web context and sets parents as ApplicationContext loaded by spring mvc ContextLoaderListener. Red5 has own context loader listener and its loads own context. There is no relation between the two context.

To override this we have to set red5 ApplicationContext as parent context of DispatcherServlet. For that you have to override initWebApplicationContext() functions in Dispatcherservlet.

I wrote a blog entry for that refer this http://www.arulraj.net/2012/04/red5-with-spring-mvc.html GIT repo is https://github.com/arulrajnet/red5Demo

There are multiple ways to access beans defined in multiple XML files

  1. You can define some XML files as "globally" available"

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

     <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/red5-web.xml </param-value> </context-param> 
  2. You can specify additional XML files of your servlet

     <servlet> <display-name>CXF Servlet</display-name> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/red5-web.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 
  3. You can import XML files into each other by

    < import resource="classpath:WEB-INF/red5-web.xml"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM