注解方式增强(实现登录日志录入数据库)(前置增强+后置增强)

LoginAspectJ中的代码

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baobaotao.domain.User;
import com.baobaotao.service.UserService;

@Aspect
public class LoginAspectJ {
	
	@Autowired
	private UserService userService;
	
	@Before("execution(* com.baobaotao.web.UserController.loginCheck(..))")
	public void beforeloginCheck(){
		System.out.println("before"+"How are u !");
	}
	
	@AfterReturning("execution(* com.baobaotao.web.UserController.loginCheck(..))")
	public void afterloginCheck(){
		System.out.println("after"+"How are u !");
		

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest();
		HttpSession  session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		if(user!=null)
		{
		userService.loginSuccess(user);
		}
	}
}


baobaotao-servlet.xml  配置部分



       
       
	
	
	
    
    


	
	
	
	
	
	
	

新增的aop部分,和xsi:schemaLocation  路径里的  aop 和 aop-3.0.xsd

你可能感兴趣的:(spring,mvc)