Spring MVC是如何找到控制器的

来源:互联网 发布:印尼社交软件 编辑:程序博客网 时间:2024/06/02 12:35

Spring使用扫描机制来找到应用程序中基于注解的控制器类。
为了保证Spring能找到控制器,需要完成两件事。
1.需要在SpringMVC的配置文件中声明sprin-context,如下:

   xmlns:context="http://www.springframework.org/schema/context" 

2.需要应用元素,如下:

   <context:component-scan base-package="mars.fw.web" /> 

在元素中指定控制器类的基本包。

下面是一个基本的配置文件示例

<?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:p="http://www.springframework.org/schema/p"    //第一步    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"    default-lazy-init="false">    <!-- 自动扫描组件,@Component @Controller @Service等这些注解的类,需要把controller去掉,否则影响事务管理 -->    //第二步    <context:component-scan base-package="mars.fw.web" />    <context:component-scan base-package="sz">        <context:exclude-filter type="regex"            expression="sz.controller.*" />    </context:component-scan>    <aop:aspectj-autoproxy/>    <!-- 引入jdbc配置文件 -->    <!-- 引入jdbc配置文件 <context:property-placeholder location="classpath:prop/jdbc.properties"        /> -->    <bean id="propertyPlaceholderConfigurer"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:prop/jdbc.properties</value>            </list>        </property>    </bean>    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 声明式事务管理 -->    <aop:config>        <aop:advisor            pointcut="execution(* sz.service..*.*(..)) || execution(* sz.job..*.*(..))"            advice-ref="myAdvice" />    </aop:config>    <tx:advice id="myAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />            <tx:method name="list*" propagation="SUPPORTS" read-only="true" />            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />            <tx:method name="create*" propagation="REQUIRED" />            <tx:method name="save*" propagation="REQUIRED" />            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="modify*" propagation="REQUIRED" />            <tx:method name="upd*" propagation="REQUIRED" />            <tx:method name="del*" propagation="REQUIRED" />            <tx:method name="excute*" propagation="REQUIRED" />            <tx:method name="*" propagation="SUPPORTS" read-only="true" />        </tx:attributes>    </tx:advice>    <!-- 创建SqlSessionFactory,同时指定数据源 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->        <property name="mapperLocations" value="classpath*:mapper/**/*.map.xml" />    </bean>    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="sz.dao" />    </bean>    <!-- 可通过注解控制事务 -->    <tx:annotation-driven />    <!-- 数据库连接池 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close">        <!-- Connection Info -->        <property name="driverClassName" value="${jdbc.driver}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <!-- Connection Pooling Info -->        <property name="maxActive" value="${dbcp.maxActive}" />        <property name="maxIdle" value="${dbcp.maxIdle}" />        <property name="defaultAutoCommit" value="false" />        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->        <property name="timeBetweenEvictionRunsMillis" value="3600000" />        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->        <property name="minEvictableIdleTimeMillis" value="3600000" />    </bean>    <!-- 国际化资源 去掉后,保存操作会出错valid is not defined -->    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource"        scope="prototype">        <property name="basenames">            <list>                <value>classpath:resource/message</value>            </list>        </property>        <property name="useCodeAsDefaultMessage" value="true" />        <property name="cacheSeconds" value="0"></property>        <property name="defaultEncoding" value="UTF-8"></property>    </bean>    <bean id="localeResolver"        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">    </bean></beans>
0 0