本文是我第一次搭建框架,做个记录,供以后复习使用。
1. web.xml(请求的起点配置)
(1).配置servlet【我们在不使用框架的情况下,一个请求需要配置一个servlet,但是使用spring框架 我们可以直接将所有的请求都转到org.springframework.web.servlet包下DispatcherServlet这个类来分发处理】
<!-- 上下文配置 通过applicationContext.xml初始化spring的容器 在spring中通过
ClassPathXmlApplicationContext加载配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*.xml</param-value>
</context-param>
<!-- 监听spring的上下文 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.test-servlet.xml(视图解析配置。springMVC相关配置)
<!-- 注解 不能少 -->
<mvc:annotation-driven />
<!-- 指定扫描的包 -->
<context:component-scan base-package ="com.controller"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
3.applicationContext.xml(spring的相关配置)
<context:annotation-config/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- aop 自动代理-->
<aop:aspectj-autoproxy/>
<!-- 包的扫描 springMVC 若不移除controller 启动报错-->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 数据源配置 ${中的内容 都会从jdbc.properties读取}-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 方言 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- hibernate映射文件的位置 classpath下xml文件夹下的所有文件-->
<property name="mappingDirectoryLocations">
<value>classpath:xml/</value>
</property>
</bean>
<!-- hibernate 是以session的方式去使用数据库的连接的 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务 -->
<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<property name="transactionManager" ref="transactionManager"></property>
<property name="txAdvice" ref="txAdvice"></property>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!-- 配置参与事务的类 -->
<!-- execution(* com.*.service.*Service.*(..)) 第一个*【执行所有方法,忽略返回值】是在
com下的所有service包里面的以Service结尾的类的任意方法 在执行时都会带进来事务 -->
<aop:config>
<aop:advisor pointcut="execution(* com.service.*Service.*(..))" advice-ref="txAdvice" />
</aop:config>