码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-09-07 12:44:09      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

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

一、配置web.xml

Xml代码 技术分享 技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.  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">  
  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/*.xml</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>  
<?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代码 技术分享 技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:aop="http://www.springframework.org/schema/aop"  
  7.        xmlns:context="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>  
<?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代码 技术分享 技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:aop="http://www.springframework.org/schema/aop"   
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"   
  6.        xmlns:tx="http://www.springframework.org/schema/tx"   
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  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>  
<?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" />
        
        <!--视图解析器-->java框架源码下载】
	    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <property name="prefix" value="/WEB-INF/jsp/" />
	        <property name="suffix" value=".jsp" />      
	    </bean>
</beans>

    

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

标签:

原文地址:http://www.cnblogs.com/dayuebings/p/4788345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!