(1)问题:如何在Web项目中配置Spring的IoC容器?
答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置:
<!-- Spring监听器 -->
<listener-class>org.springframework.web. contex t . ContextLoaderListener </listener-class>
</listener>
---------------------------------------------------------------------------------------------------------------------------------------------------
(2)如何在Web项目中配置Spring MVC?
答:要使用Spring MVC需要在Web项目配置文件中web.xml中配置其前端控制器DispatcherServlet:
<!-- Spring MVC 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:spring/spring-mvc.xml </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
===============================================================================================================
spring-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.XXXX.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 启用默认配置 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 配置Fastjson支持 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="1048576000"/>
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="10240"/>
</bean>
</beans>
===============================================================================================================
所以 <mvc:annotation-driven> 标签主要是用来帮助我们 处理请求映射,决定是哪个controller的哪个方法来处理当前请求,异常处理。
<context:component-scan/>标签:
它的实现类是org.springframework.context.annotation.ComponentScanBeanDefinitionParser.
用来扫描该包内被@Repository @Service @Controller的注解类,然后注册到工厂中。
并且context:component-scan激活@ required。@ resource,@ autowired、@PostConstruct @PreDestroy @PersistenceContext @PersistenceUnit。使得在适用该bean的时候用@Autowired就行了。主要用于对实体的注解交给spring容器管理
==============================================================================================