目前我正在使用jfinal1.5,使用的时候想结合spring将事务放置到service层中。然后在oschina中搜到了两篇帖子,所以决定自己根据帖子的内容改写下。
帖子一:http://www.oschina.net/code/snippet_188964_26555
帖子二:http://www.oschina.net/question/97474_64117 里面詹波的回答。
下面是步骤:
1、首先引入jar包,使用的是spring2.5的包,其他的jfinal使用的jar包就不用说了。
2、注释
package com.simplejj.core.tx;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
@Documented
@Inherited
public @interface TxService {
}
3、拦截器
package com.simplejj.core.tx;
import java.lang.reflect.Method;
import java.sql.SQLException;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
public class TxServiceInterceptor implements MethodInterceptor {
private final Logger logger = Logger.getLogger(TxServiceInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object target = invocation.getThis();
Method targetMethod = invocation.getMethod();
Class<?>[] targetMethodParamTypes = targetMethod.getParameterTypes();
logger.debug("当前正在对"+target.getClass().getName()+"的"+targetMethod.getName()+"方法进行拦截");
Method method = target.getClass().getMethod(targetMethod.getName(),targetMethodParamTypes);
if (method.isAnnotationPresent(TxService.class)) { // 如果存在事务的话,需要使用到jfinal提供的事务支持。
TxInvoke invoke = new TxInvoke(invocation);
Db.tx(invoke);
return invoke.getResult();
}
return invocation.proceed(); // 如果没有事务的话,直接调用方法
}
private class TxInvoke implements IAtom {
private MethodInvocation invocation;
private Object result = null;
public TxInvoke(MethodInvocation invocation) {
this.invocation = invocation;
}
@Override
public boolean run() throws SQLException {
try {
result = invocation.proceed();
return true; // 说明调用成功
} catch (Throwable e) {
logger.error("调用方法出现异常",e);
}
return false;
}
public Object getResult() {
return result;
}
}
}
package com.simplejj.frame.ap.service;
import com.simplejj.core.exception.ServiceException;
import com.simplejj.frame.ap.dao.TPdcUser;
public interface UserService {
public void update(TPdcUser user) throws ServiceException;
}
package com.simplejj.frame.ap.service.impl;
import com.simplejj.core.exception.ServiceException;
import com.simplejj.core.tx.TxService;
import com.simplejj.frame.ap.dao.TPdcUser;
import com.simplejj.frame.ap.service.UserService;
public class UserServiceImpl extends ServiceTemplate implements UserService{
public void save() throws ServiceException{
}
@TxService
public void update(TPdcUser user) throws ServiceException{
user.save();
}
}
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<import resource="classpath:spring/spring-*.xml" />
<bean id="txServiceInterceptor" class="com.simplejj.core.tx.TxServiceInterceptor"></bean>
<aop:config>
<aop:advisor id="txServiceInterceptorAdvisor" advice-ref="txServiceInterceptor" pointcut="execution(* *..service.*Service.*(..))" />
</aop:config>
</beans>
package com.simplejj;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.simplejj.frame.ap.service.UserService;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
try {
UserService userService = (UserService) ctx.getBean("userService");
userService.update(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}