spring(四)AOP相关概念

时间:2022-01-31 06:11:50

名词解释

AOP定义:面向切面编程

Joinpoint:连接点,即目标对象中,所有可以增强的方法

Pointcut:切入点,即目标对象中,已经增强的方法

Advice:通知,即需要增强的代码

Target:目标对象,即被代理的对象

Weaving:织入,将通知应用到切入点的过程

Proxy:代理,将通知织入到目标对象之后,形成代理对象

aspect:切面,切入点+通知



AOP实现原理:

1.动态代理:被代理对象必须要实现接口

2.CGLib代理:可以对任何类生成代理,代理的原理是对目标对象进行继承代理 


相关代码:

用户接口:

public interface UserService {

void add();

void delete();

void update();

void search();

}

用户接口实现类:

public class UserServiceImpl implements UserService {

@Override
public void add() {
System.out.println("add");
}

@Override
public void delete() {
System.out.println("delete");
}

@Override
public void update() {
System.out.println("update");
}

@Override
public void search() {
System.out.println("search");
}

}

通知类:

public class UserAdvice {

//前置通知:目标方法运行前调用
public void before(){
System.out.println("before");
}

//后置通知:方法出现异常不会调用此通知
public void afterReturning(){
System.out.println("afterReturning");
}

//环绕通知:目标方法执行前后都被调用
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("around before");
Object proceed = joinPoint.proceed();
System.out.println("around after");
return proceed;
}

public void afterException(){
System.out.println("afterException");
}

//后置通知:方法出现异常也调用此通知
public void after(){
System.out.println("after");
}

}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 导入aop约束 -->
<!-- 1.配置目标对象 -->
<bean name="userService" class="com.milan.spring.service.impl.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="com.milan.advice.UserAdvice"></bean>
<!-- 3.配置将通知织入目标对象 -->
<aop:config>
<!-- 配置切入点
public void com.milan.spring.service.impl.UserServiceImpl.add()
void com.milan.spring.service.impl.UserServiceImpl.add()
* com.milan.spring.service.impl.UserServiceImpl.add()
* com.milan.spring.service.impl.UserServiceImpl.*(..)
* com.milan.spring.service.impl.*ServiceImpl.*(..)
-->
<aop:pointcut expression="execution(public void com.milan.spring.service.impl.UserServiceImpl.add())" id="user_add"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="user_add" />
<aop:after-returning method="afterReturning" pointcut-ref="user_add"/>
<aop:around method="around" pointcut-ref="user_add"/>
<aop:after-throwing method="afterException" pointcut-ref="user_add"/>
<aop:after method="after" pointcut-ref="user_add"/>
</aop:aspect>
</aop:config>

</beans>

测试:

@RunWith(SpringJUnit4ClassRunner.class)//创建容器
@ContextConfiguration("classpath:applicationContext-advice.xml")//指定配置文件位置
public class AdviceTest {

@Resource(name="userService")
private UserService service;

@Test
public void Test1(){
service.add();
}

}
输出如下:

before
around before
add
after
around after
afterReturning



注释配置:

配置切面:

@Aspect
//表示该类为一个通知类
public class UserAdviceByAnnotation {

@Pointcut("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
public void advice(){}


//前置通知:目标方法运行前调用
@Before("UserAdviceByAnnotation.advice()")
public void before(){
System.out.println("before");
}

//后置通知:方法出现异常不会调用此通知
@AfterReturning("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
public void afterReturning(){
System.out.println("afterReturning");
}

//环绕通知:目标方法执行前后都被调用
@Around("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("around before");
Object proceed = joinPoint.proceed();
System.out.println("around after");
return proceed;
}

@AfterReturning("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
public void afterException(){
System.out.println("afterException");
}

//后置通知:方法出现异常也调用此通知
@After("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
public void after(){
System.out.println("after");
}

}
配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 导入aop约束 -->
<!-- 1.配置目标对象 -->
<bean name="userService" class="com.milan.spring.service.impl.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="com.milan.advice.UserAdviceByAnnotation"></bean>
<!-- 3.配置将通知织入目标对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
测试:

@RunWith(SpringJUnit4ClassRunner.class)//创建容器
@ContextConfiguration("classpath:applicationContext-advice-annotation.xml")//指定配置文件位置
public class AdviceByAnnotationTest {

@Resource(name="userService")
private UserService service;

@Test
public void Test1(){
service.add();
}

}
输出如下:

around before
before
add
around after
after
afterException
afterReturning