1、 目标类target:就是我们需要增强的那个类
2、 代理类proxy:就是自定义的代理的对象
3、 连接点joinPoint:程序执行的某个特定位置,Spring仅支持方法的连接点
4、 切入点pointCut:就是在目标类中实际增强的方法
5、 织入weave:就是将代理类中需要增强的方法放入到目标类中去执行的过程(把增强的方法放到切面中去执行
6、 引介Introduction:引介是一种特殊的增强,它为类添加一些属性和方法(课程不使用)
7、 通知advice:将代理对象中的方法应用到目标类的过程中产生的结果。
8、 切面aspect:所有的切入点和代理对象的方法组成在一起 构成了切面
这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。
Spring AOP的准备
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
1 必须要有一个接口
public interface IUser {
public void work();
}
2实现类
public class User implements IUser {
@Override
public void work() {
int i=5;
System.out.println("好好学习,天天向上");
//System.out.println(i/0);
}
3切面类
public class Advice {
public void eat(){
System.out.println("上完班使劲的吃饭");
}
@Before("execution(public void com.lyf.dao.Impl.User.work())")
public void before(){
System.out.println("before-------");
}
@AfterReturning("execution(public void com.lyf.dao.Impl.User.work())")
public void after(){
System.out.println("after-------");
}
@AfterThrowing("execution(public void com.lyf.dao.Impl.User.work())")
public void afterReturn(){
System.out.println("afterReturn-------");
}
@After("execution(public void com.lyf.dao.Impl.User.work())")
public void afterThrow(){
System.out.println("afterThrow-------");
}
public void around(ProceedingJoinPoint joinPoint){
try {
before();
// System.out.println("before-----");
joinPoint.proceed();
afterReturn();
} catch (Throwable e) {
afterThrow();
} finally {
after();
}
}
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserTest extends TestCase {
@Autowired
IUser iUser;
@Test
public void testWork() {
System.out.println(iUser);
iUser.work();
}
@Test
public void testWork2() {
System.out.println(iUser);
iUser.work();
}
}
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.lyf"></context:component-scan>
<!--<!–配置目标类对象,被增强对象,增强对象中的方法就是连接点,被增强的方法就是切点–>
<bean id="user" class="com.lyf.dao.Impl.User"></bean>
<!–配置切面类对象,增强对象–>
<bean id="advice" class="com.lyf.service.Advice"></bean>
<aop:config>
<aop:pointcut id="work" expression="execution(public void com.lyf.dao.Impl.User.work())"/>
<aop:aspect ref="advice">
<!–<aop:before method="before" pointcut-ref="work"></aop:before>
<aop:after-returning method="afterReturn" pointcut-ref="work"></aop:after-returning>
<aop:after-throwing method="afterThrow" pointcut-ref="work"></aop:after-throwing>
<aop:after method="eat" pointcut-ref="work"></aop:after>–>
<aop:around method="around" pointcut-ref="work"></aop:around>
</aop:aspect>
</aop:config>-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>