您的位置:首页 > 编程语言 > Java开发

Spring-AOP注解与方法规则实例

2016-04-08 10:36 639 查看

一、前言

在实际开发中,我们很多操作需要拦截来解决问题。例如我们老生常谈的日志和方法操作的一些统计。我这里做一个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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  aop 切面