spring-mvc 集成 activeMq 常見問題 + 解決方案 (僅供參考)


 最近整合 spring-mvc 和 activeMq ,出現了幾個異常,我把他記錄下來,具體的原理分析我就不太會寫了,只把詳細情況和解決方案給出來,希望對各位老鐵有所幫助!

 

問題1:缺少log4j的配置文件

異常信息:出現以下的內容時,可能就是你沒有配置 log4j 了。

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

 

解決方案:

在你的WEB-INF 目錄下面,添加配置文件 log4j.properties 文件, 文件配置參考如下:

log4j.rootLogger=info, Console, err

log4j.logger.play=DEBUG

# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.encoding=UTF-8
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern={ "time": "%d{yyyy-MM-dd HH:mm:ss}", "level": "%p", %m }%n

log4j.appender.err = org.apache.log4j.DailyRollingFileAppender
log4j.appender.err.encoding=UTF-8
log4j.appender.err.File = logs/appName_error.log
log4j.appender.err.Append = True
log4j.appender.err.Threshold = ERROR
log4j.appender.err.layout = org.apache.log4j.PatternLayout
log4j.appender.err.layout.ConversionPattern = { "time": "%d{yyyy-MM-dd HH:mm:ss}", "level": "%p", %m }%n
log4j.additivity.err=false

log4j.appender.info = org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.encoding=UTF-8
log4j.appender.info.File = logs/appName.log
log4j.appender.info.Append = True
log4j.appender.info.Threshold = INFO
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern = { "time": "%d{yyyy-MM-dd HH:mm:ss}", "level": "%p", %m }%n 

然后在你的 web.xml 里面添加 context-param, 代碼如下

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

  

 

 

問題2:缺少對xml 文件的 schemaLocation 設置

異常內容:

applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 14; 
columnNumber: 61; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但無法找到元素 'context:component-scan' 的聲明。

  

解決方案:

保證自己的schemaLocation 中的元素與xmlns 一致,並版本號,如果無法確定版本號是,默認用當前版本spring的。如我的 web.xml

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
">


    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <!-- 配置掃描路徑 -->
    <context:component-scan base-package="com.spring.mvc.*">
        <!-- 只掃描Service,也可以添加Repostory,但是要把Controller排除在外,Controller由spring-mvc.xml去加載 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


</beans>  

如果我將 『

http://www.springframework.org/schema/contex

http://www.springframework.org/schema/context/spring-context.xsd

』 

注釋掉,就會出現以上異常!

 

問題3:無法注入元素

異常內容:

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file 
[/Users/zeopean/IdeaProjects/springJms/target/springJms/WEB-INF/classes/spring/applicationContext-ActiveMQ.xml];
nested exception is org.springframework.beans.FatalBeanException: NamespaceHandler class
[org.apache.xbean.spring.context.v2.XBeanNamespaceHandler] for namespace
[http://activemq.apache.org/schema/core] not found;
nested exception is java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

  

解決方案:

這個問題困擾了我挺長時間的,后面通過谷歌,我發現原來是我的mvn 少了一個庫,所以,添加上就好了。具體如下

 

<dependency>
      <groupId>org.apache.xbean</groupId>
      <artifactId>xbean-spring</artifactId>
      <version>4.6</version>
    </dependency>

  

問題4:無法加載ApplicationContext-ActiveMq.xml 配置文件

異常信息:

 org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'consumerService':
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'jmsTemplate' available

  

問題分析:

我在設置 contextConfigLocation 時,錯誤的寫錯了這樣子。對!就是  classpath:spring/applicationContext.xml 少了個 * 號

,這樣子是沒辦法加載到 ApplicationContext-ActiveMq.xml的,因為總要有地方引入,配置才能注入成功嘛!

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/applicationContext.xml
        </param-value>
    </context-param>

  

解決方案:

 

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/applicationContext*.xml
        </param-value>
    </context-param>

 

好了,就到這里,希望能大家不要踩到這些坑!

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM