使用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" class="org.springframework.flex.core.MessageBrokerFactoryBean" />
但是不管用,并且即便管用,也不会解决根本问题,不过看大概的意思,明白了

报这个错误的根本原因就是,在没有使用web.xml当作Web项目进行初始化的时候,若加载Spring-BlazeDS的配置文件,就会加载相关的BlazeDS相应的监听和初始化,从而引发空指针错误


解决办法其实有很多
目的就是避免加载Spring-BlazeDS的配置文件,从而不初始化BlazeDS。

在JUnit中
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-Dao.xml" ,"/applicationContext-Service.xml"})

通过这种方法分开来写装载的配置文件,而取代通配符

再或者,使用通配符的时候,把名字和其他配置文件排除在外,使之执行不加载相应的配置文件。
-------------------------
若想使用通配符配置相应文件,则,可以在web.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>

只要这里面加载的文件和servlet配置的文件不发生冲突,不重复,就不会报错误,否则会报错

你可能感兴趣的:(spring,Flex,JUnit)