在Spring 2.0中,Pointcut的定义包括两个部分:Pointcut表示式(expression)和Pointcut签名(signature)。让我们先看看execution表示式的格式:
括号中各个pattern分别表示修饰符匹配(modifier-pattern?)、返回值匹配(ret-type-pattern)、类路径匹配(declaring-type-pattern?)、方法名匹配(name-pattern)、参数匹配((param-pattern))、异常类型匹配(throws-pattern?),其中后面跟着“?”的是可选项。
在各个pattern中可以使用“*”来表示匹配所有。在(param-pattern)中,可以指定具体的参数类型,多个参数间用“,”隔开,各个也可以用“*”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的方法;(*,String)表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数。
现在来看看几个例子:
1)execution(* *(..))
表示匹配所有方法
2)execution(public * com. savage.service.UserService.*(..))
表示匹配com.savage.server.UserService中所有的公有方法
3)execution(* com.savage.server..*.*(..))
表示匹配com.savage.server包及其子包下的所有方法
除了execution表示式外,还有within、this、target、args等Pointcut表示式。一个Pointcut定义由Pointcut表示式和Pointcut签名组成;
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
<scope>runtime</scope>
</dependency>
注解的使用:
此段小代码演示了spring aop中@Around @Before @After三个注解的区别
@Before是在所拦截方法执行之前执行一段逻辑。
@After 是在所拦截方法执行之后执行一段逻辑。
@Around是可以同时在所拦截方法的前后执行一段逻辑。
package com.itsoft.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
/**
*
* @author zxf
* 演示aop测试类
*/
@Controller
public class UserAction {
public void queryUsers(){
System.out.println("查询所有用户【all users list】");
}
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-aop.xml");
UserAction userAction = (UserAction)ctx.getBean("userAction");
userAction.queryUsers();
ctx.destroy();
}
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
*
* @author Administrator
* 通过aop拦截后执行具体操作
*/
@Aspect
@Component
public class LogIntercept {
@Pointcut("execution(public * com.itsoft.action..*.*(..))")
public void recordLog(){}
@Before("recordLog()")
public void before() {
this.printLog("已经记录下操作日志@Before 方法执行前");
}
@Around("recordLog()")
public void around(ProceedingJoinPoint pjp) throws Throwable{
this.printLog("已经记录下操作日志@Around 方法执行前");
pjp.proceed();
this.printLog("已经记录下操作日志@Around 方法执行后");
}
@After("recordLog()")
public void after() {
this.printLog("已经记录下操作日志@After 方法执行后");
}
private void printLog(String str){
System.out.println(str);
}
}
定义application-aop.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.itsoft"/>
<aop:aspectj-autoproxy />
</beans>
Spring AOP 的使用二
新建一个java类:AuthorizationAdvice ,该类的作用是调用controller时打印传入的参数:
package com.hubin.opreation;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AuthorizationAdvice {
// @Before("execution(* com.abc.service.*.many*(..))") @Around
// ProceedingJoinPoint
@Before("execution(* com.hubin.controller.*Controller.*(..))")
public void permissionCheck(JoinPoint point) throws Throwable {
System.out.println("@Before "
+ point.getSignature().getDeclaringTypeName() + "."
+ point.getSignature().getName());
System.out.println("@Before " + Arrays.toString(point.getArgs()));
Object[] args = point.getArgs();
System.out.println(args.length);
for (Object o : args) {
if (o instanceof HttpServletResponse){
HttpServletResponse response=(HttpServletResponse) o;
response.sendRedirect("/HomeController/addUser.do");
System.out.println("存在!");
}
}
}
}
在spring配置文件中配置
<context:component-scan base-package="com.hubin.opreation"></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true" />
本文深入解析Spring AOP的实现原理,包括Pointcut的定义、使用以及如何结合AspectJ进行切面编程,详细展示了如何利用注解(@Before、@After、@Around)在方法执行前后执行特定逻辑,并通过实例代码演示了Spring AOP的实践应用。
861

被折叠的 条评论
为什么被折叠?



