您的位置:首页 > 编程语言 > Java开发

基于Annotation的SpringMVC4整合Hibernate4 + freemarker

2015-10-18 21:09 946 查看
最近在学习SpringMVC 其中不少的设置与错误,记录下来也算是一些心得

首先在Eclipse 里建立一个WEB 工程,将所需要的spring freemarker Hibernate等包加入到WEB-INF文件夹下的lib文件夹下面。

要在项目中引入spring 只需要在web.xml中加入相应的配置即可。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!--  配置文件的加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/springmvc-servlet.xml,
classpath:config/spring-hibernate.xml
</param-value>
</context-param>

<!-- 监听器 ,服务器启动的时候启动MVC-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springMVC的配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 过滤Spring编码方式,防止乱码 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>


有了上面的配置,在容器启动的时候 springmvc 也就随之启动了。如果需要加入freemark,只需要将freemarker当作一个bean 注入即可。如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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.xsd"> 
<!-- 默认注解映射支持 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<!-- 自动扫描包 -->
<context:component-scan base-package="com.foxriver"  use-default-filters="true">
<!--     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />-->
</context:component-scan>

<!-- 配置freeMarker的模板路径 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="WEB-INF/template/" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- 注入freemarker视图解析器 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=UTF-8" />
<!-- 此变量值为pageContext.request, 页面使用方法:rc.contextPath -->
<property name="requestContextAttribute" value="rc" />
</bean>
</beans>
这样就可以加入 freemarker的支持了,这里需要注意的地方就是:
<pre name="code" class="html">	<context:component-scan base-package="com.foxriver"  use-default-filters="true">
</context:component-scan>



这个标签指定了 Spring自动扫描的包,Spring会自动扫描包有相关annotation注解的类,将其自动的注册为一个bean,而不用显式的在配置文件中用<bean></bean>配置。

<pre name="code" class="html">@Repository  //注解一个数据库访问组件
@Controller   //注解一个控制层的组件
@Service      //标注一个业务层组件
@Component    //当不好分类时 可以使用
@transactional  //启用事务管理




如果要注入Hiernate 只需要同样的方式 将Hibernate需要的相关组件注册成bean即可;配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:CPDATABASE" />
<property name="user" value="shopxx" />
<property name="password" value="shopxx" />
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="100 "/>
<property name="maxIdleTime" value="600" />
<property name="acquireIncrement" value="5" />
<property name="checkoutTimeout" value="60000" />
</bean>

<!--  配置hibernate SessionFactory-->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--在使用注解的情况下一定要配置这个标签,否则会报错 is not mapped-->
<property name="packagesToScan">
<list>
<!-- 可以加多个包 -->
<value>com.foxriver.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<!-- 配置事务的传播性 -->
<!--  声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager -->
<tx:advice id="txAdvice"  transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" propagation="REQUIRED" />
<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.foxriver.dao.impl..*.*(..))" />
<!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>

</beans>
然后需要理解的一点就是上面的 事务管理。为什么需要事务管理呢? 因为有时候用户的一个操作 可能对多个数据库的表进行改动:比如用户转账的时候 需要把自己的账户减少,而对方帐号要增加。假如在这个操作过程中出现了异常,导致自己的账户少了,而对方账户没有增加,这是不允许的。我们需要的是要么两个都成功,要么两个都失败。这就是事务管理。在进去操作前启用事务,操作完成关闭事务。而这一个过程 又非常符合AOP的编程思想。
AOP正好可以在不改变源代码的情况对某一个类的方法 进行前后处理。Spring 也能很好的支持AOP编程;

<aop:pointcut id="txPointcut" expression="execution(* com.foxriver.dao.impl..*.*(..))" />
以上就是配置AOP的切入点,配置的execution 表达式 就是针对配置的包类下的方法启用AOP 处理方法,这里AOP处理方法 就是Spring的事务处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: