使用Spring BlazeDS出现Error creating bean with name '_messageBroker': Invocation***
我这里的情况其实是在写Junit的时候出现的这个错误,但是应该还会有很多类似的其他情况出现这种问题,废话不多,错误如下
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_messageBroker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanInitializationException: MessageBroker initialization failed; nested exception is java.lang.NullPointerException
或
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_messageBrokerDefaultHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_messageBroker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanInitializationException: MessageBroker initialization failed; nested exception is java.lang.NullPointerException
在Spring的官网上有类似这样回答的
http://forum.springsource.org/showthread.php?71492-Issue-upgrading-to-RC1-Init-method-failed
说在spring-flex的配置文件里把
<flex:message-broker />
改成
<bean id="_messageBroker" />
但是不管用,并且即便管用,也不会解决根本问题,不过看大概的意思,明白了
报这个错误的根本原因就是,在没有使用web.xml当作Web项目进行初始化的时候,若加载Spring-BlazeDS的配置文件,就会加载相关的BlazeDS相应的监听和初始化,从而引发空指针错误
解决办法其实有很多
目的就是避免加载Spring-BlazeDS的配置文件,从而不初始化BlazeDS。
在JUnit中
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "/applicationContext-Dao.xml" ,"/applicationContext-Service.xml"})
<servlet><servlet-name>Spring flex Dispatcher Servlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:flexContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Spring flex Dispatcher Servlet</servlet-name><url-pattern>/messagebroker/*</url-pattern></servlet-mapping>
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:conf/spring/applicationContext-Dao.xml,classpath*:conf/spring/applicationContext-Service.xml,classpath*:conf/spring/applicationContext-XXX.xml,</param-value></context-param>
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>