方式一:基于aspectj的xml配置实现
1、除了导入基本spring的jar以外还需要导入aop所需jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
2、创建spring核心配置文件,并导入aop的约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd " >
</beans>
3、使用表达式配置切入点
- execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
示例1:execution(* com.wyj.UserService.add(..))当前类指定方法
示例2:execution(* com.wyj.UserService.*(..))当前类所有方法
示例3:execution(* *.*(..))所有类所有方法
示例4:execution(* *.save*(..))所有类所有以save开头法人方法
-
- 访问修饰符:public、private、protect、*(通配符)
4、配置
- 配置两个对象
public class AopService {
private String name;
public void setName(String name) {
this.name = name;
}
public void sayBye() {
// TODO Auto-generated method stub
System.out.println("Bye "+name);
}
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello "+name);
}
}
public class AopServiceAdvice{
public void before() {
System.out.println("*调用方法之前*");
}
public void after() {
System.out.println("**调用方法之后**");
}
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("***执行方法之前***");
proceedingJoinPoint.proceed();
System.out.println("***执行方法之后***");
}
}
- 配置两个基本bean并注入属性值
<bean id="aopService" class="com.wyj.RealizeAOP.model.AopService">
<property name="name" value="吴玉军"></property>
</bean>
<bean id="aopServiceAdvice" class="com.wyj.RealizeAOP.model.AopServiceAdvice"></bean>
- 配置aop操作
<!-- aop配置 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.wyj.RealizeAOP.model.AopService.*(..))" id="pointcutAdd"/>
<!-- 配置切面 把通知 织入到 切入点的过程-->
<aop:aspect ref="aopServiceAdvice">
<aop:before method="before" pointcut-ref="pointcutAdd"/>
<aop:around method="around" pointcut-ref="pointcutAdd"/>
<aop:after method="after" pointcut="execution(* com.wyj.RealizeAOP.model.AopService.sayHello(..))"/>
</aop:aspect>
</aop:config>
- 测试和结果展示
@Test
public void aopBaseTheory() {
ApplicationContext ac = new ClassPathXmlApplicationContext("conf/spring.xml");
AopService aopService = (AopService)ac.getBean("aopService");
aopService.sayHello();
aopService.sayBye();
}
结果
*调用方法之前*
***执行方法之前***
Hello 吴玉军
***执行方法之后***
**调用方法之后**
*调用方法之前*
***执行方法之前***
Bye 吴玉军
***执行方法之后***
方式一:基于aspectj的注解实现
1、创建对象
<bean id="aopService" class="com.wyj.RealizeAOP.model.AopService">
<property name="name" value="吴玉军"></property>
</bean>
<bean id="aopServiceAdvice" class="com.wyj.RealizeAOP.model.AopServiceAdvice"></bean>
2、在spring核心配置文件中,开启aop操作
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3、在增强类使用注解实现
@Aspect
public class AopServiceAdvice{
@Before(value="execution(* com.wyj.RealizeAOP.model.AopService.sayHello(..))")
public void before() {
System.out.println("*调用方法之前*");
}
@After(value="execution(* com.wyj.RealizeAOP.model.AopService.sayHello(..))")
public void after() {
System.out.println("**调用方法之后**");
}
@Around(value="execution(* com.wyj.RealizeAOP.model.AopService.sayHello(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("***执行方法之前***");
proceedingJoinPoint.proceed();
System.out.println("***执行方法之后***");
}
}
4、测试和结果展示
@Test
public void aopBaseTheory() {
ApplicationContext ac = new ClassPathXmlApplicationContext("conf/spring.xml");
AopService aopService = (AopService)ac.getBean("aopService");
aopService.sayHello();
}
***执行方法之前***
*调用方法之前*
Hello 吴玉军
***执行方法之后***
**调用方法之后**