SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结


下载地址:

http://pan.baidu.com/s/1qWDinyk

一 开发环境

1、动态web工程

2、部分依赖

    hibernate-release-4.1.0.Final.zip  
    hibernate-validator-4.2.0.Final.jar  
    spring-framework-3.1.1.RELEASE-with-docs.zip  
    proxool-0.9.1.jar  
    log4j 1.2.16  
    slf4j -1.6.1  
    mysql-connector-java-5.1.10.jar  
    hamcrest 1.3.0RC2  
    ehcache 2.4.3  

、为了方便学习,暂没有使用maven构建工程

二 工程主要包括内容

1、springMVC + spring3.1.1 + hibernate4.1.0集成

2、通用DAO层 和 Service层

3、二级缓存 Ehcache

3.1:缓存了数据库查询

3.2:实现了页面缓存

    ehcache-core-2.5.2.jar

    ehcache-web-2.0.4.jar 主要针对页面缓存

4、REST风格的表现层

REST功能是Spring MVC 3.0新增的,它通过不带扩展名的URL来访问系统资源。(http://www.cnblogs.com/crazylqy/p/4323520.html)

5、通用分页(两个版本)

5.1、首页 上一页,下一页 尾页 跳转

5.2、上一页 1 2 3 4 5 下一页

6、数据库连接池采用proxool

7、spring集成测试

8、表现层的 java validator框架验证(采用hibernate-validator-4.2.0实现)

9、视图采用JSP,并进行组件化分离

10、xss过滤器

11、使用gzip优化web应用(filter实现)

http://www.cnblogs.com/crazylqy/p/4326299.html

三 TODO LIST 将本项目做成脚手架方便以后新项目查询

1、Service层进行AOP缓存(缓存使用Memcached实现)

2、单元测试(把常见的桩测试、伪实现、模拟对象演示一遍 区别集成测试)

3、监控功能

后台查询hibernate二级缓存 hit/miss率功能

后台查询当前服务器状态功能(如 线程信息、服务器相关信息)

4、spring RPC功能

5、spring集成 quartz 进行任务调度

6、spring集成 java mail进行邮件发送

7、DAO层将各种常用框架集成进来(方便查询)

8、把工作中经常用的东西 融合进去,作为脚手架,方便以后查询

四 集成重点及常见问题

1spring-config.xml配置文件:

1.1、该配置文件只加载除表现层之外的所有bean,因此需要如下配置:

    <context:component-scan base-package="cn.javass">  
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    </context:component-scan>  

通过exclude-filter 把所有 @Controller注解的表现层控制器组件排除

1.2、国际化消息文件配置

    <!-- 国际化的消息资源文件 -->  
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
            <property name="basenames">  
                <list>  
                    <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找  -->  
                    <value>classpath:messages</value>  
                </list>  
            </property>  
            <property name="defaultEncoding" value="UTF-8"/>  
            <property name="cacheSeconds" value="60"/>  
        </bean>  

此处basenames内一定是 classpath:messages ,如果你写出“messages”,将会到你的web应用的根下找 即你的messages.properties一定在 web应用/messages.propertis。

1.3、hibernate的sessionFactory配置 需要使用org.springframework.orm.hibernate4.LocalSessionFactoryBean,其他都是类似的,具体看源代码。

1.4、<aop:aspectj-autoproxy expose-proxy="true"/> 实现@AspectJ注解的,默认使用AnnotationAwareAspectJAutoProxyCreator进行AOP代理,它是 BeanPostProcessor的子类,在容器启动时Bean初始化开始和结束时调用进行AOP代理的创建,因此只对当容器启动时有效,使用时注意此 处。

1.5、声明式容器管理事务

建议使用声明式容器管理事务,而不建议使用注解容器管理事务(虽然简单),但太分布式了,采用声明式容器管理事务一般只对service层进行处理。

    <tx:advice id="txAdvice" transaction-manager="txManager">  
        <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(* cn.javass..service..*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>  
    </aop:config>  

此处一定注意 使用 hibernate4,在不使用OpenSessionInView模式时,在使用getCurrentSession()时会有如下问题:

当有一个方法list 传播行为为Supports,当在另一个方法getPage()(无事务)调用list方法时会抛出 org.hibernate.HibernateException: No Session found for current thread 异常。

这是因为getCurrentSession()在没有session的情况下不会自动创建一个,不知道这是不是Spring3.1实现的bug,欢迎大家讨论下。

因此最好的解决方案是使用REQUIRED的传播行为。

二、spring-servlet.xml

2.1、表现层配置文件,只应加装表现层Bean,否则可能引起问题。

    <!-- 开启controller注解支持 -->  
    <!-- 注:如果base-package=cn.javass 则注解事务不起作用-->  
    <context:component-scan base-package="cn.javass.demo.web.controller">  
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    </context:component-scan>  

此处只应该加载表现层组件,如果此处还加载dao层或service层的bean会将之前容器加载的替换掉,而且此处不会进行AOP织入,所以会造成AOP失效问题(如事务不起作用),再回头看我们的1.4讨论的。

2.2、<mvc:view-controller path="/"view-name="forward:/index"/> 表示当访问主页时自动转发到index控制器。

2.3、静态资源映射

    <!-- 当在web.xml 中   DispatcherServlet使用     <url-pattern>/</url-pattern> 映射时,能映射静态资源 -->  
    <mvc:default-servlet-handler/>  
    <!-- 静态资源映射 -->  
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />  
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />  
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />  

以上是配置文件部分,接下来来看具体代码。

三、通用DAOHibernate4实现

为了减少各模块实现的代码量,实际工作时都会有通用DAO层实现,以下是部分核心代码:

    public abstract class BaseHibernateDao<M extends java.io.Serializable, PK extends java.io.Serializable> implements IBaseDao<M, PK> {  
       
        protected static final Logger LOGGER = LoggerFactory.getLogger(BaseHibernateDao.class);  
       
        private final Class<M> entityClass;  
        private final String HQL_LIST_ALL;  
        private final String HQL_COUNT_ALL;  
        private final String HQL_OPTIMIZE_PRE_LIST_ALL;  
        private final String HQL_OPTIMIZE_NEXT_LIST_ALL;  
        private String pkName = null;  
       
        @SuppressWarnings("unchecked")  
        public BaseHibernateDao() {  
            this.entityClass = (Class<M>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];  
            Field[] fields = this.entityClass.getDeclaredFields();  
            for(Field f : fields) {  
                if(f.isAnnotationPresent(Id.class)) {  
                    this.pkName = f.getName();  
                }  
            }  
             
            Assert.notNull(pkName);  
            //TODO @Entity name not null  
            HQL_LIST_ALL = "from " + this.entityClass.getSimpleName() + " order by " + pkName + " desc";  
            HQL_OPTIMIZE_PRE_LIST_ALL = "from " + this.entityClass.getSimpleName() + " where " + pkName + " > ? order by " + pkName + " asc";  
            HQL_OPTIMIZE_NEXT_LIST_ALL = "from " + this.entityClass.getSimpleName() + " where " + pkName + " < ? order by " + pkName + " desc";  
            HQL_COUNT_ALL = " select count(*) from " + this.entityClass.getSimpleName();  
        }  
             
        @Autowired  
        @Qualifier("sessionFactory")  
        private SessionFactory sessionFactory;  
       
        public Session getSession() {  
            //事务必须是开启的,否则获取不到  
            return sessionFactory.getCurrentSession();  
        }  
    ……  
    }  

Spring3.1集成Hibernate4不再需要HibernateDaoSupport和HibernateTemplate了,直接使用原生API即可。

四、通用Service层代码此处省略,看源代码,有了通用代码后CURD就不用再写了。

    @Service("UserService")  
    public class UserServiceImpl extends BaseService<UserModel, Integer> implements UserService {  
       
        private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);  
       
        private UserDao userDao;  
       
        @Autowired  
        @Qualifier("UserDao")  
        @Override  
        public void setBaseDao(IBaseDao<UserModel, Integer> userDao) {  
            this.baseDao = userDao;  
            this.userDao = (UserDao) userDao;  
        }  
         
       
       
        @Override  
        public Page<UserModel> query(int pn, int pageSize, UserQueryModel command) {  
            return PageUtil.getPage(userDao.countQuery(command) ,pn, userDao.query(pn, pageSize, command), pageSize);  
        }  
    }  
       

五、表现层Controller实现

采用SpringMVC支持的REST风格实现,具体看代码,此处我们使用了java Validator框架 来进行 表现层数据验证

在Model实现上加验证注解

@Pattern(regexp = "[A-Za-z0-9]{5,20}", message = "{username.illegal}") //java validator验证(用户名字母数字组成,长度为5-10)  
private String username;  
  
@NotEmpty(message = "{email.illegal}")  
@Email(message = "{email.illegal}") //错误消息会自动到MessageSource中查找  
private String email;  
  
@Pattern(regexp = "[A-Za-z0-9]{5,20}", message = "{password.illegal}")  
private String password;  
  
@DateFormat( message="{register.date.error}")//自定义的验证器  
private Date registerDate; 

在Controller中相应方法的需要验证的参数上加@Valid即可

    @RequestMapping(value = "/user/add", method = {RequestMethod.POST})  
    public String add(Model model, @ModelAttribute("command") @Valid UserModel command, BindingResult result)  

六、Spring集成测试

使用Spring集成测试能很方便的进行Bean的测试,而且使用 @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)能自动回滚事务,清理测试前后状态。

    @RunWith(SpringJUnit4ClassRunner.class)  
    @ContextConfiguration(locations = {"classpath:spring-config.xml"})  
    @Transactional  
    @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)  
    public class UserServiceTest {  
         
        AtomicInteger counter = new AtomicInteger();  
         
        @Autowired  
        private UserService userService;  
        ……    
    }  

七、ehcache缓存

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="javass">

    <diskStore path="d://ehcache"/>

<!-- maxElementsInMemory :cache 中最多可以存放的元素的数量。如果放入cache中的元素超过这个数值,有两种情况:
1、若overflowToDisk的属性值为true,会将cache中多出的元素放入磁盘文件中。
2、若overflowToDisk的属性值为false,会根据memoryStoreEvictionPolicy的策略替换cache中原有的元素。 -->
   <defaultCache
      maxElementsInMemory="1000"
      eternal="false"
      timeToIdleSeconds="3600"
      timeToLiveSeconds="3600"
      overflowToDisk="false">
    </defaultCache>

   <cache name="cn.javass.demo.model.UserModel"
      maxElementsInMemory="2000"
      eternal="false"
      timeToIdleSeconds="3600"
      timeToLiveSeconds="3600"
      overflowToDisk="false">
    </cache>

<!-- 设置默认的查询缓存区域 -->
<!-- 该配置应该写,否则会出现警告,若不写等于没有用查询缓存 -->
    <cache
        name="org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory="5000"
        eternal="false"
        timeToLiveSeconds="3600"
        overflowToDisk="false"/>

<!-- 设置时间戳缓存区域 -->
<!-- 该配置应该写,否则会出现警告,若不写等于没有用查询缓存 -->
    <cache
        name="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        overflowToDisk="true"/>
    
    <cache name="SimplePageCachingFilter" 
        maxElementsInMemory="10000" 
        eternal="false"
        overflowToDisk="false" 
        timeToIdleSeconds="900" 
        timeToLiveSeconds="1800"
        memoryStoreEvictionPolicy="LFU" />
   
</ehcache>

spring-config.xml配置

                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
                <prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
                <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>

1.数据库缓存

1.1

   <cache name="cn.javass.demo.model.UserModel"
      maxElementsInMemory="2000"
      eternal="false"
      timeToIdleSeconds="3600"
      timeToLiveSeconds="3600"
      overflowToDisk="false">
    </cache>

<!-- 设置默认的查询缓存区域 -->
<!-- 该配置应该写,否则会出现警告,若不写等于没有用查询缓存 -->
    <cache
        name="org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory="5000"
        eternal="false"
        timeToLiveSeconds="3600"
        overflowToDisk="false"/>

<!-- 设置时间戳缓存区域 -->
<!-- 该配置应该写,否则会出现警告,若不写等于没有用查询缓存 -->
    <cache
        name="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        overflowToDisk="true"/>

1.2

1.3

2.页面缓存

2.1

    <cache name="SimplePageCachingFilter" 
        maxElementsInMemory="10000" 
        eternal="false"
        overflowToDisk="false" 
        timeToIdleSeconds="900" 
        timeToLiveSeconds="1800"
        memoryStoreEvictionPolicy="LFU" />

2.2

package cn.javass.common.web.filter;

import java.util.Enumeration;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.constructs.blocking.LockTimeoutException;
import net.sf.ehcache.constructs.web.AlreadyCommittedException;
import net.sf.ehcache.constructs.web.AlreadyGzippedException;
import net.sf.ehcache.constructs.web.filter.FilterNonReentrantException;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class PageEhCacheFilter extends SimplePageCachingFilter {

    private final static Logger log = Logger.getLogger(PageEhCacheFilter.class);
    private final static String FILTER_URL_PATTERNS = "patterns";
    private static String[] cacheURLs;
    private void init() throws CacheException {
        String patterns = filterConfig.getInitParameter(FILTER_URL_PATTERNS);
        cacheURLs = StringUtils.split(patterns, ",");
    }

    @Override
    protected void doFilter(final HttpServletRequest request,
    final HttpServletResponse response, final FilterChain chain)
    throws AlreadyGzippedException, AlreadyCommittedException,
    FilterNonReentrantException, LockTimeoutException, Exception {
        if (cacheURLs == null) {
            init();
        }
        String url = request.getRequestURI();
        boolean flag = false;
        if (cacheURLs != null && cacheURLs.length > 0) {
            for (String cacheURL : cacheURLs) {
                if (url.contains(cacheURL.trim())) {
                    flag = true;
                    break;
                }
            }
        }

        // 如果包含我们要缓存的url 就缓存该页面,否则执行正常的页面转向

        if (flag) {
            String query = request.getQueryString();
            if (query != null) {
                query = "?" + query;
            }
            log.info("当前请求被缓存:" + url + query);
            super.doFilter(request, response, chain);
        } else {
            chain.doFilter(request, response);
        }
    }

    @SuppressWarnings("unchecked")
    private boolean headerContains(final HttpServletRequest request,
            final String header, final String value) {
        logRequestHeaders(request);
        final Enumeration accepted = request.getHeaders(header);
        while (accepted.hasMoreElements()) {
            final String headerValue = (String) accepted.nextElement();
            if (headerValue.indexOf(value) != -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 
     * @see net.sf.ehcache.constructs.web.filter.Filter#acceptsGzipEncoding(javax.servlet.http.HttpServletRequest)
     * 
     *      <b>function:</b> 兼容ie6/7 gzip压缩
     * 
     * @author hoojo
     * 
     * @createDate 2012-7-4 上午11:07:11
     */

    @Override
    protected boolean acceptsGzipEncoding(HttpServletRequest request) {
        boolean ie6 = headerContains(request, "User-Agent", "MSIE 6.0");
        boolean ie7 = headerContains(request, "User-Agent", "MSIE 7.0");
        return acceptsEncoding(request, "gzip") || ie6 || ie7;

    }

}

2.3 web.xml

            <!-- 缓存、使用页面缓存,gzip压缩核心过滤器 -->
            <filter>
                <filter-name>PageEhCacheFilter</filter-name>
                <filter-class>cn.javass.common.web.filter.PageEhCacheFilter</filter-class>
                <init-param>
                    <param-name>patterns</param-name>
                    <!-- 配置你需要缓存的url -->
                    <param-value>/pageCache.jsp,/pageCache </param-value>
                </init-param>
            </filter>
            <filter-mapping>
                <filter-name>PageEhCacheFilter</filter-name>
                <url-pattern>/pageCache</url-pattern>
            </filter-mapping>

2.4测试

package cn.javass.demo.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @author Zhangkaitao
 * @version 1.0
 */
@Controller
public class pageCacheController {
    
    @RequestMapping(value = "/pageCache")
    public String index(HttpServletRequest request){
        return "pageCache";
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.Date"%>
<%@ include file="inc/header.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
</head>
<body>
<p>使用new Date()来检测该页面是否被缓存</p>
<%=new Date()%>
</body>
</html>

访问两次该页面,检测时间没有发生变化,说明该url被缓存了

八、xss过滤器

使用XssRequestWrapper装饰器模式

页面缓存可参考:http://www.cnblogs.com/crazylqy/p/4325363.html

其他部分请直接看源码

优质内容筛选与推荐>>
1、UVa1583 Digit Generator
2、用例建模Use Case Modeling
3、javaFX制作helloWorld的几种方式
4、Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)
5、POJ 3126-Prime Path(BFS)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn