Spring学习–2 aop
演示了aop的两种拦截方式:基于注解规则拦截和基于方法规则进行拦截。
步骤:添加依赖->编写基于注解拦截的规则->被拦截类->切面->java配置类->bootrap类
添加依赖(pom.xml)
<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.test</groupId>
<artifactId>com-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</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>
基于注解拦截的规则
package com.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() default "";
}
被拦截的类
1、基于注解拦截的类
package com.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
@Action(name="注解方式拦截的方法")
public void test() {
System.out.println("This is a test in DemoAnnotationService.");
}
}
2、基于方法拦截的类
package com.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void test() {
System.out.println("This is a test in DemoMethodService.");
}
}
切面
package com.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(com.aop.Action)")
public void annotationPointcut() {}
// @Pointcut("execution(* com.aop.DemoMethodService.*(..))")
// public void methodPointcut() {}
@After("annotationPointcut()")
public void after(JoinPoint point) {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println("注解式拦截:" + action.name());
}
@Before("execution(* com.aop.DemoMethodService.*(..))")
public void before(JoinPoint point) {
MethodSignature signature = (MethodSignature)point.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则拦截:" + method.getName());
}
}
Java配置类
package com.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.aop")
@EnableAspectJAutoProxy
public class AopConfig {
}
启动类bootrap
package com.aop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Bootrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService annotationService =
context.getBean(DemoAnnotationService.class);
DemoMethodService methodService =
context.getBean(DemoMethodService.class);
annotationService.test();
methodService.test();
context.close();
}
}