首先咱们来了解一下具体的业务场景(这个跟第一篇中的很相似但有不同):具体的业务是这样的,现在系统中有六十多个主档(功能模块),每个主档都有新增、修改、删除功能,当我们在对每个主档做这些操作时需要对其记录日志,需要注意的是,修改的时候不对数据库中的数据做任何修改,所以这里就用到了拦截器(当然也可以使用hibernate监听,而且应该会简单点,有兴趣的同志可以试一下),对修改方法进行拦截同时拿到修改方法的参数,代码如下(这里只写Spring bean配置和bean实现类,具体的业务bean就是serviceImpl里面的add、update、delete):
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="masterFileService" abstract="true" class="com.yueya.oms.lmd.CommitLog.impl.CommitLogFactoryBean">
</bean>
<bean id="myStoreTypeCodeService" class="com.yueya.oms.lmd.StoreTypeCode.impl.StoreTypeCodeServiceImpl"/>
<bean id="storeTypeCodeService" parent="masterFileService">
<!--代理接口-->
<property name="serviceInterface" value="com.yueya.oms.lmd.StoreTypeCode.StoreTypeCodeService"/>
<!--目标实现类-->
<property name="serviceImpl" ref="myStoreTypeCodeService"/>
<!--目标对象执行方法-->
<property name="methodName" value="updateStoreTypeCode"/>
</bean>
....
</beans>
FactoryBean的实现
public class CommitLogFactoryBean<T> implements FactoryBean<T>, InitializingBean, MethodInterceptor {
private Object proxy;//代理对象
private Class<Object> serviceInterface;//代理接口
private Object serviceImpl;//目标实现类
private String methodName;//目标对象执行方法
private ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
public CommitLogFactoryBean() {
}
public Object getProxy() {
return proxy;
}
public void setProxy(Object proxy) {
this.proxy = proxy;
}
public Class<Object> getServiceInterface() {
return serviceInterface;
}
public void setServiceInterface(Class<Object> serviceInterface) {
this.serviceInterface = serviceInterface;
}
public Object getServiceImpl() {
return serviceImpl;
}
public void setServiceImpl(Object serviceImpl) {
this.serviceImpl = serviceImpl;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
@Override
public T getObject() throws Exception {
return (T) this.proxy;
}
@Override
public Class<?> getObjectType() {
return this.serviceInterface;
}
@Override
public boolean isSingleton() {
return false;
}
public void afterPropertiesSet() throws Exception {
this.proxy = (new ProxyFactory(this.serviceInterface, this)).getProxy(this.classLoader);
}
/**
* 当主档做修改操作时将修改方法拦截且不对数据库执行任何操作,同时将拦截的数据记入CommitLog中,
* 若是新增和删除则在对数据库执行完新增和删除操作后将新增和删除的数据记入CommitLog中
*
* @param invocation
* @return
* @throws Throwable
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
Method method = invocation.getMethod();
String methodName = method.getName();
Object result = null;
Object[] objects = invocation.getArguments();
if (methodName.startsWith("add") || methodName.startsWith("delete")) {//新增或删除
result = $invoke(serviceImpl, method, objects);
RecordContext.getInstance().take(objects[0], methodName, result);
} else if (methodName.equals(this.methodName)) {//修改
RecordContext.getInstance().take(objects[0], methodName, "");
} else if (methodName.equals("toString")) {//调试时使用
return this.serviceImpl.toString();
} else {//查询
result = $invoke(serviceImpl, method, objects);
}
return result;
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
private Object $invoke(Object target, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
return method.invoke(target, args);
}
}
切记,在这里要手动调用invoke()方法执行目标方法,否则无法执行,具体原因我也不太清楚,有兴趣的同志可以研究研究,顺便教我下。