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 配置部分
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 扫描web包,应用Spring的注解 -->
<context:component-scan base-package="com.baobaotao.web"/>
<context:component-scan base-package="com.baobaotao.dao"/>
<context:component-scan base-package="com.baobaotao.service"/>
<context:component-scan base-package="com.baobaotao.aspectj"/>
<aop:aspectj-autoproxy/>
<bean id="userservice" class="com.baobaotao.service.UserService"/>
<bean class="com.baobaotao.aspectj.LoginAspectJ"/>
<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
新增的aop部分,和xsi:schemaLocation 路径里的 aop 和 aop-3.0.xsd