我的这个例子是一个登录的例子:
用到Spring的Aop,在login()方法后用Aop来拦截,将用户登录信(username,password)写到ThreadLocal中,然后在其它地方再取出来用
代码如下:
LoginServlet.java
package com.yimei.drp.servlet.login;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yimei.drp.common.AuthCurrentUser;
import com.yimei.drp.model.user.UserInfo;
import com.yimei.drp.service.user.IUserService;
public class LoginServlet extends HttpServlet implements Servlet{
private IUserService userService;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
userService.login("zj","123456");
UserInfo userInfo = (UserInfo) AuthCurrentUser.get();
System.out.println("===用户==="+userInfo.getUsername());
System.out.println("===密码==="+userInfo.getPassword());
request.getRequestDispatcher("/pages/login/main.jsp").forward(request, response);
}
}
LoginAspect.java
package com.yimei.drp.aop.login;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import com.yimei.drp.common.AuthCurrentUser;
import com.yimei.drp.model.user.UserInfo;
@Aspect
public class LoginAspect {
//@Pointcut("execution(* com.yimei.drp.service.user.IUserService.login(..))")
//public void loginMethod(){};
@Before("execution(* com.yimei.drp.service.user.IUserService.login(..))")
//@Before(value="loginMethod()")
public void loginBefore(){
System.out.println("登录前");
}
@AfterReturning("execution(* com.yimei.drp.service.user.IUserService.login(..)) && args(username,password,..)")
// //@AfterReturning(value="loginMethod() && args(username,..)")
public void loginAfter(String username,String password){
System.out.println(username+"已成功登录了");
UserInfo userInfo=new UserInfo();
userInfo.setUsername(username);
userInfo.setPassword(password);
AuthCurrentUser.set(userInfo);
}
}
这个是关键
AuthCurrentUser.java
package com.yimei.drp.common;
import com.yimei.drp.model.user.UserInfo;
public class AuthCurrentUser {
/**
* 声明局部线程变量
*/
private static ThreadLocal<UserInfo> currentUser = new ThreadLocal<UserInfo>();
/**
* 设置当前用户对象
*
* @param user 用户值对象
*/
public static void set(UserInfo user) {
currentUser.set(user);
}
/**
* 获取当前用户对象
*
* @return 当前用户对象
*/
public static UserInfo get() {
return (UserInfo) currentUser.get();
}
}
aop的配置文件
<?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:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy />
<bean id="loginAspect" class="com.yimei.drp.aop.login.LoginAspect" />
</beans>