一、基于自定义注解@Pointcut方式
1.自定义一个注解
package spring4.java.service.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/***
*
* 自定义注解
*
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
2.创建使用1注解的方法类
package spring4.java.service.aop;
import org.springframework.stereotype.Service;
/**
*
*
* 根据自定义的注解,给所在的bean的特殊的方法标识上注解标识,好让切点反射处理方法过滤到
*
*/
@Service
public class DemoAnnotetionService {
@Action(name="自定义的注解拦截方法add")
public void add(){
System.out.println("调用注解方法类DemoAnnotetionService方法add:");
}
}
3.创建横切拦截类,进行拦截
package spring4.java.service.aop;
import java.lang.reflect.Method;
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;
@Aspect
@Component
public class LogAspect {
/**
* 对指定注解,进行横切,创建一个横切的对象方法
*
*/
@Pointcut("@annotation(spring4.java.service.aop.Action)")
public void annotationPoint(){};
/**
* 对横切方法,进行反射处理,对使用了注解方法“前”:不仅可以捕捉到注解内容,还有方法名等,
* 此处的作用主要是:可以对使用注解使用的方法,进行方法特殊逻辑处理(可以具体到哪个方法使用了哪个注解内容进行特殊处理)
*
*/
@Before("annotationPoint()")
public void BeforeAnnotation(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
Action action=method.getAnnotation(Action.class);
System.out.println("注解的拦截方法名注解内容前:"+action.name());
}
/**
* 对横切方法,进行反射处理,对使用了注解方法“后”:不仅可以捕捉到注解内容,还有方法名等,
* 此处的作用主要是:可以对使用注解使用的方法,进行方法特殊逻辑处理(可以具体到哪个方法使用了哪个注解内容进行特殊处理)
*
*/
@After("annotationPoint()")
public void AfterAnnotation(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
Action action=method.getAnnotation(Action.class);
System.out.println("注解的拦截方法名注解内容后:"+action.name());
}
}
二、包下拦截方法使用execution
package spring4.java.service.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void add(){
System.out.println("调用方法类DemoMethodService方法add:");
}
}
2.创建注解类
/**
* 对包下类的所有方法执行前,进行横切
*
*/
//注意,*需要空格,不然将会报错
@Before("execution(* spring4.java.service.aop.DemoMethodService.*(..))")
public void BeforeDemoMethod (JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
//此处可以根据一些特殊的方法进行处理逻辑
System.out.println("方法拦截的方法名字前:"+method.getName());
}
//注意,*需要空格,不然将会报错
@After("execution(* spring4.java.service.aop.DemoMethodService.*(..))")
public void AfterDemoMethod (JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
//此处可以根据一些特殊的方法进行处理逻辑
System.out.println("方法拦截的方法名字后:"+method.getName());
}
三,其中配置类:
package spring4.java.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import spring4.java.service.part1.FunctionService;
import spring4.java.service.part1.UseFunctionService;
import spring4.java.service.part2.FunctionService2;
import spring4.java.service.part2.UseFunctionService2;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan( basePackages={“spring4.java”},
excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}
)
@EnableAspectJAutoProxy//开启切面的支持
public class RootConfig {
}
四、通过Controller调用及其结果
package spring4.java.controller.part1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import spring4.java.service.aop.DemoAnnotetionService;
import spring4.java.service.aop.DemoMethodService;
import spring4.java.service.part1.UseFunctionService;
import spring4.java.service.part2.UseFunctionService2;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.beans.factory.annotation.Autowired;
@Controller
@RequestMapping("/logAspect")
public class LogAspectController {
@Autowired
private DemoAnnotetionService demoAnnotetionService;
@Autowired
private DemoMethodService demoMethodService;
@RequestMapping(value = "/aspect", method=GET)
public ModelAndView usemethod() {
System.out.println("Annotion test!!");
demoAnnotetionService.add();
demoMethodService.add();
String message = "Annotion test from";
return new ModelAndView("use", "message", message);
}
}