在后端逻辑开发过程中,必然有数据的字段是重复的,是需要保存的,比如当前用户修改了订单,需要记录修改人,修改时间等信息至数据库中,此时没有引用hibernate拦截器时,是需要在每个方法里面都添加对应的代码,记录修改人,修改时间等等字段信息,对这些公用信息每个修改操作都得重复复制,比如在大型电子商务网站中,就必然涉及到很多操作,表结构成百上千,就得在成百上千的后台代码都复制同样的代码进行赋值。
先不说工作量多少,没多少工作量,就是复制下就可。至少这个会多很多不必要的代码,对于系统架构来说,这个就需要在一开始就处理好。此时正好hibernate拦截器就出现了。同时这个拦截器不会对系统造成任何影响,可以随时剔除,同时也不会对框架整体性造成错误影响,完全是个即插即用的。
package com.todaysteel.esaasfront.whs.common; import java.io.Serializable; import java.util.Date; import org.apache.log4j.Logger; import org.hibernate.EmptyInterceptor; import org.hibernate.type.Type; import org.springframework.stereotype.Component; import com.todaysteel.core.utils.security.SpringSecurityUtils; import com.todaysteel.esaasfront.publiccloud.commom.security.vo.Authorization; /** * @author liuhaixiao * hibernate拦截器: * 实现创建人,创建时间自动注入; * 修改人,修改时间自动注入; */ @Component public class PropertyInterceptor extends EmptyInterceptor { private static final Logger log = Logger.getLogger(PropertyInterceptor.class); private static final long serialVersionUID = 1L; @Override public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { log.info("------Hibernate Interceptor-----onSave-----"); Authorization user = SpringSecurityUtils.getCurrentUser(); //添加数据 for (int index=0;index<propertyNames.length;index++) { //找到名为"添加日期"的属性 if ("dadddate".equals(propertyNames[index])) { //使用拦截器将对象的"添加日期"属性赋上值 if(state[index]==null){ state[index] = new Date(); } continue; } else if ("saddoperator".equals(propertyNames[index])) {//找到名为"添加人"的属性 //使用拦截器将对象的"添加人"属性赋上值 if(state[index]==null){ state[index] = user.getUsername(); } continue; } } return true; } @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { log.info("------Hibernate Interceptor-----onFlushDirty-----"); Authorization user = SpringSecurityUtils.getCurrentUser(); //添加数据 for (int index=0;index<propertyNames.length;index++) { /*找到名为"修改时间"的属性*/ if ("dmodifydate".equals(propertyNames[index])) { /*使用拦截器将对象的"修改时间"属性赋上值*/ currentState[index] = new Date(); continue; } else if ("smodifyoperator".equals(propertyNames[index])) {/*找到名为"修改人"的属性*/ /*使用拦截器将对象的"修改人"属性赋上值*/ currentState[index] = user.getUsername(); continue; } } return true; } }
hibernate提供了很多拦截器,在实际需要用到时才查看源码,类似于spring的AOP机制。
同理对电子商务网站需要监控日志的内容同样可以用此拦截器进行跟踪用户的行为。
当然最好是用AOP来处理跟踪用户的日志行为。
由于我是采用配置的方式来采用拦截器,所以需要在xml中进行配置。
<!-- 自定义hibernate属性拦截器 --> <bean id ="proInterceptor" class="com.todaysteel.esaasfront.whs.common.PropertyInterceptor" /> <!-- Hibernate配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="DynamicDataSource" /> <property name="namingStrategy"> <bean class="org.hibernate.cfg.ImprovedNamingStrategy" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop> </props> </property> <property name="packagesToScan" value="com.todaysteel.esaasfront,com.todaysteel.core.business" /> <property name="entityInterceptor" ref="proInterceptor"/> </bean>
我把这个拦截器的作用域是放在session级别的,所以配置在sesisonFactory上。
相关推荐
使用拦截器时按如下步骤进行: (1)定义实现Interceptor接口的拦截器类 (2)通过Session启用拦截器,或者通过Configuration启用全局拦截器请看示例代码
JavaWeb项目,SpringMVCHibernate实例【含登录拦截器例子】
主要讲述Hibernate的核心API,幻灯片演示,很清晰。
在hibernate中实现oracle的自动增长
spring4+hibernate4+springmvc4+ztree+maven nexus管理支持ztree,以及分页,泛型Dao
拦截器和过滤器的区别 1、拦截器基于动态代理 , 过滤器基于函数回调 2、拦截器不依赖于servlet容器,通过动态代理实现,过滤器依赖于servlet容器 3、拦截器在方法前后,异常前后等调用,而过滤器只能在请求前和请求...
1.针对SpringMVC注解的配置,可起到入门的作用 2.Spring+JdbcTemplate事物管理 3.Spring+Hibernate事物管理 ...5.Spring配置拦截器 6.Spring通过拦截器实现防止重复提交实例 对学习,会起到非常好的效果
2012最新ssh2框架-struts2.3+hibernate4.1+spring3.1登录+注册(md5加密)带拦截器,mysql数据库,带分页功能, 只需修改com/resource/resourses.properties数据库连接即可使用
在工程的配置文件中填写数据库密码的密文,在应用程序使用datasource的时候解密成明文以创建连接。
Struts拦截器的案例,没有使用Hibernate,使用的是JDBC,该代码的核心实现是拦截器,至于数据库可根据个人需求进行修改。
hibernate注入的三种方式
使用spring+struts2+hibernate实现简单的登录拦截器。实现登录。用户注销功能。单表增删查实现分页功能
自己整合的很全面的框架 flex struct2 spring hibernate 拦截器 过滤器 真的很超值 挣分是小 交流很重要
使用Hibernate拦截器检测Persistence Context中的实体更改 使用Hibernate拦截器对数据库中的实体更改做出React 使用观察者设计模式来监视实体持久性更改并对其做出React 使用命令设计模式提供一种通用方法,可将...
完整可运行起来的SSH项目!中使用了SAIF 插件 实现拦截器的效果!并且有sql语句 !我使用的是mysql数据库,如果你使用别的数据库请改配置文件!运行成功后注意控制台的输出
在创建/更新实体期间使用Hibernate拦截器强制执行租户详细信息。 使用Spring AOP(AspectJ)设置过滤器参数。 说明: 每个请求都经过一个自定义的servlet过滤器,该过滤器检查X-TenantID http标头,并使用...
Hibernate 显示完整sql的配置工具,配置好后在Console会输出不带?的sql语句,方便调试。
应用程序已注册了一个请求拦截器和一个Hibernate拦截器,它们对执行的查询进行计数并记录下来。 计数也显示在模型中,因此统计信息可以显示在页面本身上。 有关此工作方式的详细信息,请参见以下博客文章: 想法是...
spring=非标准的J2EE技术实现(很多开源的Framwork)。 Sun标准:J2EE技术,Servlet、JSP、JPA、JTA、...struts+spring+hibernate webwork+spring+hibernate struts+spring+ibatis struts+spring+jdbc webWork+spring+
基于Maven的Struts2与Hibernate的入门级代码实例,实现了拦截器、Json等等多个实例。