(@Transactional)和数据缓存(@Cacheable)等上都使用了此种方式的拦截
添加spirng aop支持和AspectJ依赖
<!--spring的aop支持--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!--aspectj支持--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency>
编写拦截规则的注解
package jack.ch1.aop; import java.lang.annotation.*; /** * Created by wj on 2017/7/8. */ /** *自定义注解,注解本身是没有功能的,就和xml一样。注解和xml都是一种元数据,元数据就是解释数据的数据,这就是所谓的配置 * 注解的功能来自调用这个注解的地方 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }
编写使用注解的被拦截类:
package jack.ch1.aop; import org.springframework.stereotype.Service; /** * Created by wj on 2017/7/8. * 使用注解的被拦截类 */ @Service public class DemoAnnotationService { @Action(name = "注解式拦截的add操作") public void add(){} }
编写使用方法规则被拦截类:
package jack.ch1.aop; import org.springframework.stereotype.Service; /** * Created by wj on 2017/7/8. * 使用方法的规则的被拦截类 */ @Service public class DemoMethodService { public void add(){} }
编写切面:
package jack.ch1.aop; 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 wj on 2017/7/8. * 切面 * @Aspect声明切面 * @Component让此切面成为spring容器管理的bean */ @Aspect @Component public class LogAspect { //@Pointcut注解声明切点 @Pointcut("@annotation(jack.ch1.aop.Action)") public void annotationPointCut(){} /** * 使用注解式的拦截 * @After声明一个建言,并使用 @Pointcut定义的切点 * @param joinPoint */ @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()); } /** * @Before注解声明一个建言,此建言直接使用拦截规则作为参数 * @param joinPoint */ @Before("execution(* jack..ch1.aop.DemoMethodService.*(..))") public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method =signature.getMethod(); System.out.println("方法规则拦截: "+method.getName()); } }
配置类:
package jack.ch1.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * Created by wj on 2017/7/8. * @EnableAspectJAutoProxy注解开启spring对aspectj的支持 */ @Configuration @ComponentScan("jack.ch1.aop") @EnableAspectJAutoProxy public class AopConfig { }
测试代码如下:
package jack.ch1.aop; import jack.ch1.bean.JavaConfig; import jack.ch1.bean.UseFunctionService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by jack on 2017/6/25. */ public class MainTest3 { public static void main(String [] args){ //AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); context.close(); } }
测试结果如下:
下面是完整的pom.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jack</groupId> <artifactId>springstudy1</artifactId> <version>1.0-SNAPSHOT</version> <!--定义属性--> <properties> <java.version>1.8</java.version> </properties> <!--添加依赖--> <dependencies> <!--添加spring框架依赖包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!--spring的aop支持--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!--aspectj支持--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!--增加commons-io可简化文件相关操作--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> </dependencies> <!--添加插件--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>
