一、导入相关的Jar包
导入的Jar包有
spring-aop、spring-context、aspectjweaver
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.zbt</groupId>
<artifactId>spring_aop</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring_aop</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>
二、编写切面
通过注解的方式实现切面
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class Audience {
@Before("execution(* com.zbt.day01.Performance.perform(..))")
public void silenceCellphones(){
System.out.println("观众手机静音");
}
@Before("execution(* com.zbt.day01.Performance.perform(..))")
public void takeSeats(){
System.out.println("观众入座");
}
@AfterReturning("execution(* com.zbt.day01.Performance.perform(..))")
public void applause(){
System.out.println("观众鼓掌");
}
@AfterThrowing("execution(* com.zbt.day01.Performance.perform(..))")
public void demandRefund(){
System.out.println("观众要求退款");
}
}
三、编写切点
package com.zbt.day01;
public class Performance {
public void perform(){
System.out.println("表演开始了");
}
}
四、使用XML配置的方式装配Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<bean id="performance" class="com.zbt.day01.Performance"></bean>
<bean id="audience" class="com.zbt.day01.Audience"></bean>
</beans>
五、测试
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestI {
@Test
public void testPerformance(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
Performance performance = context.getBean("performance",Performance.class);
performance.perform();
}
}
输出结果
观众手机静音
观众入座
表演开始了
观众鼓掌
六、参考
1、《Spring实战-Spring in Action》-张卫滨