简单拦截器(spring aop)

1.applicationContext.xml

http://www.springframework.org/schema/beans"
xmlns:http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance"
xmlns:http://www.springframework.org/schema/p">http://www.springframework.org/schema/p"
xmlns:http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop"
xmlns:http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx"
xsi:schemaLhttp://www.springframework.org/schema/beans">http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">








org.hibernate.dialect.MySQLDialect






com/edong/beian/model/Log.hbm.xml






class="org.springframework.orm.hibernate3.HibernateTransactionManager">












PROPAGATION_REQUIRED,-RuntimeException







lazy-init="default" autowire="default" dependency-check="default"
class="com.edong.beian.service.LogService">








logService




interceptorTrans





















before
after
around
throw





2. cglib:实现前增强,后增强,前后增强,异常增强 (四个单独的类)
前增强
import com.edong.ws.security.Login;
import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.MethodBeforeAdvice;
public class LogMethodBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] aobj, Object obj)
throws Throwable {
String date = (new Date()).toString();
Login login=null;
if(aobj.length>0){
if(aobj[0] instanceof Login){
login =(Login)aobj[0];
System.out.println("信息:[" + date + "]用户 " +login.getUsername() + " 正在尝试登录陆系统...");
}
}
}
后增强...
import com.edong.ws.security.Login;
import java.lang.reflect.Method;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.aop.AfterReturningAdvice;
public class LogAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object obj, Method method, Object[] aobj,
Object obj1) throws Throwable {
String date = (new Date()).toString();
/ /防止其他方法进入:logService.getCount();方法中就没参数...所以不存在访问数组元素...就会报错..
if(aobj.length>0){
if(aobj[0] instanceof Login){
Login login=(Login)aobj[0];
System.out.println("信息:["+date+"]用户 "+login.getUsername()+" 成功登录系统.");
}
}
}
前后增强
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class LogMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Object result = null;
Object[] args = methodInvocation.getArguments();//获取传入目标方法的参数列表
int length =args.length;
for (int i = 0; i if (args instanceof Session){//判断参数类型是否是 Session
Transaction trans=((Session)(args)).beginTransaction();
try {
trans.begin();
result = methodInvocation.proceed();//调用代理目标类的具体方法
trans.commit();
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
}
}
}
return result;
}
}

异常增强 :导包
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ThrowExceptionAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target,Exception ex)throws Throwable {
System.out.println("用户登录过程中发生异常: "+target.getClass().getSimpleName());
}
}


测试类:导包
import com.edong.beian.service.LogService;
import com.edong.ws.security.Login;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
public static void main(String[] args){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");//xml文件 放在src目录下
LogService logService = (LogService) ac.getBean("proxy1");
Login login=new Login();
login.setUsername("thh");
login.setPassword("aaa");
logService.clientLogin(login);
}
}

你可能感兴趣的:(spring2.0)