ssm项目配置文件详解以及缺少配置引发的惨案
1 案例描述
使用ssm整合做一个简单的登录拦截功能实现。代码写完开始运行,噩梦就开始了,各种错误各种百度。
2 报错信息及解决办法
Error creating bean with name 'userController' defined in file [D:xxx\UserController.class]:
// 重要部分
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
- 原因:经百度知道是因为Controller层的bean的注入使用的构造器注入,但没有手写无参构造方法,好,我重写
private UserService userService;
public UserController() {
}
public UserController(UserService userService) {
this.userService = userService;
}
重新运行,哈哈哈哈,不报错了,开始测试,输入http://localhost:8080/main,成功拦截跳转到登录页面!
你以为结束了吗?并没有!继续输入数据库中用户名密码登录,问题来了!
服务器端报500异常了,提示空指针异常,断点调试,哦是UserService没有注入成功,原来加上空的构造方法默认是注入空构造器,需要用@AutoWired注解指定注入有参构造器!那简单,我加就是了。
private UserService userService;
public UserController() {
}
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
重启服务器。新的错误出现了(心态逐渐崩了)
Error creating bean with name 'userController' defined in file [D:xxx\UserController.class]:
// 这句不管了,空构造器已经加了
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
// 继续看后面信息:没有可用的userserive的bean,应该还是bean配置的问题
No qualifying bean of type 'com.itheima.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
看到这个错误,开始了漫长的查看三个配置文件(applicationContext.xml、spring-mvc.xml、mybatis-config.xml)的内容,三个!我为什么不查看四个呢?
又过了好久,配置没有问题。一早上过去了,放弃不干了!
下午在总结ssm整合环境配置的时候突然发现web.xml中需要配置Spring的监听器,仔细一想,早上的项目web.xml一直没有检查,果然!只有一个spring-mvc的试图解析器配置!!好了破案了,加上开始重启,问题解决!
<!--spring监听器-->
<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>
3 总结
- 碰到错误不用盲目复制一部分上百度,仔细阅读报错信息,虽然是英文的,但是比百度可太有效了。
- 规范配置文件写法,一定要规范!(虽然ssm开发不怎么用了,还是别忘了吧)
- 出错了别一直钻牛角尖了,适当干的别的事情。
4 ssm配置文件详细信息
applicationContext.xml:配置数据源、开启service注解扫描、开启Mapper代码开发MapperScannerConfigurer、配置事务管理器、开启事务注解、配置MyBatis工厂
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--1. 读取jdbc配置文件,配置数据源-->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2. 开启注解扫描-->
<context:component-scan base-package="com.itheima.service" />
<!--3. Mapper代理开发-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper" />
</bean>
<!--4. 配置事务管理器,依赖于数据源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--5. 开启事务注解-->
<tx:annotation-driven/>
<!--6. 配置MyBatis工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource" />
<!--指定核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
</beans>
注:在实际开发中,为了避免配置文件信息过于臃肿,通常会将Spring配置文件中的信息按照不同功能分散在多个配置文件中。例如可以将事务配置在名称为applicationcontext-transaction.xml文件中,将数据源信息放置在ApplicationContext-db.xml中。这样web.xml配置Spring文件信息时,只需要通过applicationContext-*.xml的方式即可自动加载全部配置信息。
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--自定义别名-->
<typeAliases>
<package name="com.itheima.po"/>
</typeAliases>
<!--加载映射文件-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
由于在Spring中已经配置了数据源信息以及mapper接口文件扫描器,所以在MyBatis的配置文件中只需要进行别名配置即可。
springmvc-config.xml:配置包扫描器,扫描@Controller注解、加载注解驱动、配置视图解析器
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:conext="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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--1. 扫描controller-->
<conext:component-scan base-package="com.itheima.controller" />
<!--2. 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--3. mvc注解驱动-->
<mvc:annotation-driven/>
</beans>
web.xml:配置文件监听器、编码过滤器,以及Spring MVC的前端控制器等信息
<!--spring监听器-->
<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>
<!--springmvc前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>