你的位置:首页 > Java教程

[Java教程]搭建基于全注解的Spring+Spring MVC+Hibernate框架


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

一、配置web.

 收藏代码

  1. <?version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" "http://java.sun.com/
  3.  "http://www.w3.org/2001/xsi:schemaLocation="http://java.sun.com/>  
  4.    
  5.  <!-- 配置log4j日志   -->  
  6.  <context-param>  
  7.   <param-name>log4jConfigLocation</param-name>  
  8.   <param-value>/WEB-INF/classes/log4j.properties</param-value>  
  9.  </context-param>  
  10.  <listener>  
  11.   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  12.  </listener>  
  13.    
  14.  <!-- 字符集过滤器 -->  
  15.  <filter>  
  16.   <filter-name>characterEncodingFilter</filter-name>  
  17.   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  18.   <init-param>  
  19.    <param-name>encoding</param-name>  
  20.    <param-value>UTF-8</param-value>  
  21.   </init-param>  
  22.  </filter>  
  23.  <filter-mapping>  
  24.   <filter-name>characterEncodingFilter</filter-name>  
  25.   <url-pattern>/*</url-pattern>  
  26.  </filter-mapping>  
  27.    
  28.  <!-- 配置SpringMVC -->  
  29.  <servlet>  
  30.   <servlet-name>dispatcher</servlet-name>  
  31.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  32.   <!-- 初始化参数   -->  
  33.   <init-param>  
  34.    <param-name>contextConfigLocation</param-name>  
  35.    <param-value>/WEB-INF/classes/spring/*.</param-value>  
  36.   </init-param>  
  37.   <load-on-startup>1</load-on-startup>  
  38.  </servlet>  
  39.  <servlet-mapping>  
  40.   <servlet-name>dispatcher</servlet-name>  
  41.   <!-- 表示拦截所有/XX的请求  -->  
  42.   <url-pattern>/</url-pattern>  
  43.  </servlet-mapping>  
  44.    
  45.  <welcome-file-list>  
  46.   <welcome-file></welcome-file>  
  47.  </welcome-file-list>  
  48.    
  49. </web-app>  

<?

二、配置applicationContext.

 收藏代码

  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.        "http://www.w3.org/2001/
  4.        "http://www.springframework.org/schema/p"  
  5.        "http://www.springframework.org/schema/tx"  
  6.        "http://www.springframework.org/schema/aop"  
  7.        "http://www.springframework.org/schema/context"  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.        http://www.springframework.org/schema/tx  
  11.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.        http://www.springframework.org/schema/aop  
  13.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  14.        http://www.springframework.org/schema/context  
  15.        http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
  16.        default-init-method="init">      
  17.        
  18.     <!-- 引入jdbc配置文件 -->  
  19.     <context:property-placeholder location="classpath:jdbc.properties" />  
  20.       
  21.     <!-- 配置数据源 -->  
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  23.         <property name="driverClassName" value="${driver}" />  
  24.         <property name="url" value="${url}" />  
  25.         <property name="username" value="${username}" />  
  26.         <property name="password" value="${password}" />  
  27.     </bean>  
  28.    
  29.     <!-- 配置SessionFactory -->  
  30.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  31.         <property name="dataSource" ref="dataSource"/>  
  32.         <property name="hibernateProperties">  
  33.             <props>  
  34.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>  
  35.                 <prop key="show_sql">true</prop>  
  36.                 <prop key="format_sql">true</prop>  
  37.             </props>  
  38.         </property>  
  39.         <property name="packagesToScan">  
  40.             <list>  
  41.                 <value>com.xieke.test.entity</value><!-- 扫描实体类 -->  
  42.             </list>  
  43.         </property>  
  44.     </bean>  
  45.       
  46.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">    
  47.         <property name="sessionFactory" ref="sessionFactory" />    
  48.     </bean>  
  49.   
  50.     <!-- 配置事务管理器,使用拦截器声明式事务  -->  
  51.     <bean id="transactionManager"  
  52.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  53.         <property name="sessionFactory" ref="sessionFactory" />  
  54.     </bean>  
  55.     <bean id="transactionInterceptor"    
  56.         class="org.springframework.transaction.interceptor.TransactionInterceptor">    
  57.         <property name="transactionManager" ref="transactionManager" />    
  58.         <!-- 配置事务属性 -->    
  59.         <property name="transactionAttributes">    
  60.             <props>    
  61.                 <!-- PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务  -->  
  62.                 <prop key="add*">PROPAGATION_REQUIRED</prop>  
  63.                 <prop key="del*">PROPAGATION_REQUIRED</prop>  
  64.             </props>    
  65.         </property>    
  66.     </bean>  
  67.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    
  68.         <property name="beanNames">    
  69.             <list>    
  70.                 <value>*ServiceImpl</value>  
  71.             </list>    
  72.         </property>    
  73.         <property name="interceptorNames">    
  74.             <list>    
  75.                 <value>transactionInterceptor</value>    
  76.             </list>    
  77.         </property>    
  78.     </bean>    
  79.    
  80. </beans>  

<?

三、配置dispatcher-servlet.

 收藏代码

  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"   
  3.        "http://www.springframework.org/schema/aop"   
  4.        "http://www.springframework.org/schema/context"  
  5.        "http://www.springframework.org/schema/mvc"   
  6.        "http://www.springframework.org/schema/tx"   
  7.        "http://www.w3.org/2001/
  8.        xsi:schemaLocation="http://www.springframework.org/schema/aop   
  9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
  10.         http://www.springframework.org/schema/beans   
  11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  12.         http://www.springframework.org/schema/context   
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  14.         http://www.springframework.org/schema/mvc   
  15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
  16.         http://www.springframework.org/schema/tx   
  17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  18.           
  19.         <!-- 自动扫描注解组件并且自动注入 -->  
  20.         <context:component-scan base-package="com.xieke.test" />  
  21.           
  22.         <!-- 开启注解 -->  
  23.         <mvc:annotation-driven />  
  24.           
  25.         <!-- 激活默认的静态资源处理器 -->  
  26.         <mvc:default-servlet-handler/>  
  27.           
  28.         <!-- 配置spring mvc的默认首页  -->  
  29.         <mvc:view-controller path="/" view-name="redirect:/car/index" />  
  30.           
  31.         <!--视图解析器-->  
  32.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  33.             <property name="prefix" value="/WEB-INF/jsp/" />  
  34.             <property name="suffix" value=".jsp" />        
  35.         </bean>  
  36. </beans>  

<?
	  <bean id="viewResolver" >	    <property name="prefix" value="/WEB-INF/jsp/" />	    <property name="suffix" value=".jsp" />   	  </bean></beans>