在SSM框架中,我遇到過的一些問題及其解決方法,以後如果還遇到問題會繼續跟新。
SSM框架——常見問題及解決方案(2)
一、
問題描述
Could not open ServletContext resource [/WEB-INF/Dispatcher-servlet.xml]
解決方案
- 注意< param-name>裏的值必須是 contextConfigLocation
- 添加< init-param>< /init-param>的內容(否則默認在/WEB-INF/下)
如圖:resources文件爲classpath
<!-- SpringMVC的前端控制器,攔截所有請求 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!--配置服務器啓動後立即加載Spring MVC配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
二、
問題描述
Error:Cannot build artifact ‘ghostPlanner:war exploded’ because it is included into a circular dependency
解決方案
項目重命名之後有可能會出現這種問題,只要刪除多餘的Artifact即可。
三、
問題描述
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
解決方案
1、Mapper.xml和Mapper.java的名稱是否一致,是否在同一個包下面
2、mapper.xml的namespace配置是否準確
3、mapper.java中的方法是否都存在於mapper.xml中
4、mapper.xml裏< select>元素是否正確配置了ResultMap或ResultType (< select>元素詳解)
5、mapper的xml路徑是否配置正確
6、target目錄下是否有mapper對應的xml文件,如果沒有,在pom文件中添加如下信息:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
命名錯誤真的很要命,找半天都找不到哪裏寫錯了,現在都是寫了一個名稱之後,全部複製粘貼,無論名字多短,真是怕了…
四、
問題描述
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisUtil': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.StringRedisTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
解決方案
這個錯誤主要是兩個原因:
- @Autowired 使用默認類型注入。把@Autowired 改成 @Resource
- applicationContext.xml 配置錯誤,使用了註解的類不在掃描的包中。
保險一點的做法就是把所有包都掃描,但這樣無疑會降低效率。
<context:component-scan base-package = "java.*"/>