1. spring 做junit测试时,需要从配置文件中创建ApplicationContext对象
整合到web项目中时,通过配置ContextLoaderListener来装配ApplicationContext的配置信息。
applictionContext的作用:
作用1:责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责bean的声明周期。
作用2:提供更完整的框架功能:
a. 国际化支持
b. 资源访问:Resource rs = ctx. getResource(“classpath:config.properties”),“file:c:/config.properties”
c. 事件传递:通过实现ApplicationContextAware接口
contextLoaderListener的作用(web.xml中):
启动Web容器时,自动装配ApplicationContext的配置信息。
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
contextLoaderListener一般配合contextConfigLocation一起使用,来加载spring配置文件,否则会默认查找/WEB-INF/applicationContext.xml路径下的配置文件。
---------至此spring在web.xml中的配置结束--------------------------------
2. 配置springmvc
springmvc的核心控制器:DispatcherServlet
contextConfigLocation配置文件路径
配置拦截器
3. 配置乱码编码
<!-- 乱码解决 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>