文章目录
  1. 1. 介绍Spring4中使用AOP
    1. 1.1. pom文件
    2. 1.2. 代码实现
    3. 1.3. 执行结果

介绍Spring4中使用AOP

spring4中通过编码配置使用AOP

pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

代码实现

这里使用是通过编码配置的方式实现,具体如下:

拦截器规则(注解规则)

1
2
3
4
5
6
7
8
9
10
import java.lang.annotation.*;
/**
* 编写拦截器规则的注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}

测试Service

1
2
3
4
5
6
7
//注解式
@Service
public class DemoAnnotationService {
@Action(name="注解式拦截的add操作")
public void add(){}
}
1
2
3
4
5
6
//方法式
@Service
public class DemoMethodService {
public void add() {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
@Aspect //通过注解申明了一个切面
@Component //通过注解让切面成为Spring容器管理的Bean
public class LogAspect {
@Pointcut("@annotation(com.knight.ch1.aop.Action)") //通过注解申明切点
public void annotationPointCut(){}
@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("execution(* com.knight.ch1.aop.DemoMethodService.*(..))") //使用拦截规则作为参数
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截 "+method.getName());
}
}

AOP配置类

1
2
3
4
5
6
7
8
9
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.knight.ch1.aop")
@EnableAspectJAutoProxy // 注解开启Spring对AspectJ代理的支持
public class AopConfig {
}

启动类

1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
context.close();
}
}

执行结果

AOP执行结果

文章目录
  1. 1. 介绍Spring4中使用AOP
    1. 1.1. pom文件
    2. 1.2. 代码实现
    3. 1.3. 执行结果