当前位置: 代码迷 >> VC/MFC >> 筹建基于全注解的Spring+Spring MVC+Hibernate框架
  详细解决方案

筹建基于全注解的Spring+Spring MVC+Hibernate框架

热度:174   发布时间:2016-05-02 03:51:50.0
搭建基于全注解的Spring+Spring MVC+Hibernate框架

以实例讲解Spring+Spring MVC+Hibernate框架搭建步骤:

?

一、配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <!-- 配置log4j日志   --> <context-param>  <param-name>log4jConfigLocation</param-name>  <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <listener>  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>  <!-- 字符集过滤器 --> <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> </filter> <filter-mapping>  <filter-name>characterEncodingFilter</filter-name>  <url-pattern>/*</url-pattern> </filter-mapping>  <!-- 配置SpringMVC --> <servlet>  <servlet-name>dispatcher</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- 初始化参数   -->  <init-param>   <param-name>contextConfigLocation</param-name>   <param-value>/WEB-INF/classes/spring/*.xml</param-value>  </init-param>  <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>  <servlet-name>dispatcher</servlet-name>  <!-- 表示拦截所有/XX的请求  -->  <url-pattern>/</url-pattern> </servlet-mapping>  <welcome-file-list>  <welcome-file></welcome-file> </welcome-file-list> </web-app>

二、配置applicationContext.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:p="http://www.springframework.org/schema/p"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-2.5.xsd"       default-init-method="init">             <!-- 引入jdbc配置文件 -->    <context:property-placeholder location="classpath:jdbc.properties" />        <!-- 配置数据源 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName" value="${driver}" />        <property name="url" value="${url}" />        <property name="username" value="${username}" />        <property name="password" value="${password}" />    </bean>     <!-- 配置SessionFactory -->	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">		<property name="dataSource" ref="dataSource"/>		<property name="hibernateProperties">			<props>				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>				<prop key="show_sql">true</prop>				<prop key="format_sql">true</prop>			</props>		</property>		<property name="packagesToScan">			<list>				<value>com.xieke.test.entity</value><!--?扫描实体类?-->			</list>		</property>	</bean>		<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">          <property name="sessionFactory" ref="sessionFactory" />      </bean>	<!-- 配置事务管理器,使用拦截器声明式事务  -->	<bean id="transactionManager"	    class="org.springframework.orm.hibernate3.HibernateTransactionManager">	    <property name="sessionFactory" ref="sessionFactory" />	</bean>    <bean id="transactionInterceptor"          class="org.springframework.transaction.interceptor.TransactionInterceptor">          <property name="transactionManager" ref="transactionManager" />          <!-- 配置事务属性 -->          <property name="transactionAttributes">              <props>              	<!-- PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务  -->                <prop key="add*">PROPAGATION_REQUIRED</prop>                <prop key="del*">PROPAGATION_REQUIRED</prop>            </props>          </property>      </bean>    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">          <property name="beanNames">              <list>                  <value>*ServiceImpl</value>            </list>          </property>          <property name="interceptorNames">              <list>                  <value>transactionInterceptor</value>              </list>          </property>      </bean>   </beans>

三、配置dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"        xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"        xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">                <!-- 自动扫描注解组件并且自动注入 -->   		<context:component-scan base-package="com.xieke.test" />                <!-- 开启注解 -->		<mvc:annotation-driven />                <!-- 激活默认的静态资源处理器 -->        <mvc:default-servlet-handler/>                <!-- 配置spring mvc的默认首页  -->	    <mvc:view-controller path="/" view-name="redirect:/car/index" />                <!--视图解析器-->	    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">	        <property name="prefix" value="/WEB-INF/jsp/" />	        <property name="suffix" value=".jsp" />      	    </bean></beans>

? ??

? ?至此搭建完成,以上配置都是主要配置,详细搭建过程请下载源码:http://pan.baidu.com/s/1dD57a6H

???转载请注明出处:?http://xieke90.iteye.com/blog/2241035

? ?

  相关解决方案