web.xml 配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring.xml;
classpath:config/spring-mybatis.xml;
classpath:config/spring-shiro.xml;
classpath:config/activiti.cfg.xml;
</param-value>
</context-param>
<listener>
<description>spring監(jiān)聽(tīng)器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
此配置是使用 ContextLoaderListener 告訴他Spring 配置文件的位置。
監(jiān)聽(tīng)器是 配置上下文載入器、此載入器是是載入除了DispacherServlet 載入的配置文件的其他上下文配置文件
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
spring-mvc.xml
<!-- 自動(dòng)掃描的包名 -->
<context:component-scan base-package="com.app,com.core,JUnit4" >
</context:component-scan>
<context:component-scan base-package="sinosoft.framework; sinosoft.platform; sinosoft.project">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<!-- 默認(rèn)的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 視圖解釋類 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/><!--可為空,方便實(shí)現(xiàn)自已的依據(jù)擴(kuò)展名來(lái)選擇視圖解釋類的邏輯 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
<!-- 攔截器 -->
<mvc:interceptors>
<bean class="com.core.mvc.MyInteceptor" />
</mvc:interceptors>
<!-- 對(duì)靜態(tài)資源文件的訪問(wèn) 方案一 (二選一) -->
<mvc:default-servlet-handler/>
<!-- 對(duì)靜態(tài)資源文件的訪問(wèn) 方案二 (二選一)-->
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
<!-- 上傳文件的配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="50000000" />
</bean>
<!--國(guó)際化 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean class="sinosoft.framework.core.interceptor.GlobalInterceptor" />
</mvc:interceptors>
<context:component-scan/> 掃描指定的包中的類上的注解,常用的注解有:
@Controller 聲明Action組件
@Service 聲明Service組件 @Service("myMovieLister")
@Repository 聲明Dao組件
@Component 泛指組件, 當(dāng)不好歸類時(shí).
@RequestMapping("/menu") 請(qǐng)求映射
@Resource 用于注入,( j2ee提供的 ) 默認(rèn)按名稱裝配,@Resource(name="beanName")
@Autowired 用于注入,(srping提供的) 默認(rèn)按類型裝配
@Transactional( rollbackFor={Exception.class}) 事務(wù)管理
@ResponseBody
@Scope("prototype") 設(shè)定bean的作用域