Spring-AOP注解与方法规则实例
一、前言
在实际开发中,我们非常多操作须要拦截来解决这个问题。比如我们老生常谈的日志和方法操作的一些统计。我这里做一个aop样例。以供自己和大家有用起来方便。
本文主要有两种方式解决aop:1、有用注解 2、有用方法规则方法
两种方式都是基于java文件的。为什么没有基于xml。由于xml已经out于太繁琐,比較不是全项目的拦截,所以不用xml配置。
项目链接地址:点击打开链接 https://github.com/yangchangyong0/springaopTest
二、使用注解实现aop
2.1 首先编写注解类
package com.ycy.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by ycy on 16/4/8.
* 编写拦截的注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {
String name();
}
2.2 对须要拦截方法加注解
package com.ycy.annotation;
import org.springframework.stereotype.Service;
/**
* Created by ycy on 16/4/8.\
* 对须要注解的方法加上注解
*/
@Service
public class DemoAnnotationService {
@Action(name = "注解连接add方法操作")
public void add(){
System.out.println("DemoAnnotationService运行add方法");
}
}三、用法规则实现aop
3.1 直接编写类(不须要不论什么其它操作)
package com.ycy.method;
import org.springframework.stereotype.Service;
/**
* Created by ycy on 16/4/8.
* 直接编写类,不须要其它不论什么操作
*/
@Service
public class DemoMethodService {
public void add(){
System.out.println("DemoMethodService运行add方法");
}
}
四、aop切面编写
在切面中。我们使用after 与before 。将两种拦截切面都做了拦截。
package com.ycy.aop;
import com.ycy.annotation.Action;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
//
/**
* Created by ycy on 16/4/8.
*/
@Aspect//1
@Component//2
public class LogAspect {
/*拦截累切点编写*/
@Pointcut("@annotation(com.ycy.annotation.Action)")//3
public void annotationPointCut(){
System.out.println("開始拦截--可是不打印");
}
/*使用注解方法拦截*/
@After("annotationPointCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
Method method=signature.getMethod();
Action action=method.getAnnotation(Action.class);
System.out.println("注解拦截方式:"+action.name());//5
}
/*用法规则拦截*/
@Before("execution(* com.ycy.method.DemoMethodService.*(..))")//6
public void before(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
Method method=signature.getMethod();
System.out.println("方法规则拦截方式:"+method.getName());
}
}
在使用spring框架配置AOP的时候,无论是通过XML配置文件还是注解的方式都须要定义pointcut"切入点" 比如定义切入点表达式 execution(* com.sample.service.impl..*.*(..)) execution()是最经常使用的切点函数,其语法例如以下所看到的: 整个表达式能够分为五个部分: 1、execution(): 表达式主体。 2、第一个*号:表示返回类型,*号表示全部的类型。 3、包名:表示须要拦截的包名。后面的两个句点表示当前包和当前包的全部子包。com.sample.service.impl包、子孙包下全部类的方法。 4、第二个*号:表示类名,*号表示全部的类。 5、*(..):最后这个星号表示方法名。*号表示全部的方法,后面括弧里面表示方法的參数,两个句点表示不论什么參数。
五、測试代码
package com.ycy.main;
import com.ycy.annotation.DemoAnnotationService;
import com.ycy.method.DemoMethodService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* Created by ycy on 16/4/8.
* 1使用扫描注解方法建立context
* 然后运行我们的两个測试方法
*/
@Configuration
@ComponentScan("com.ycy")
@EnableAspectJAutoProxy//1
public class AopconfigTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopconfigTest.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
}
}
測试结果输出
DemoAnnotationService运行add方法 注解拦截方式:注解连接add方法操作 方法规则拦截方式:add DemoMethodService运行add方法 Process finished with exit code 0

浙公网安备 33010602011771号