如果本代码有疑问,请访问spring-aop快速入门或者spring-aop动态代理技术(底层分析)
1.导入aop的相关坐标
<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.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2.创建目标接口和目标类
public interface TargetInterface {
public void save();
}
public class Target implements TargetInterface{
public void save() {
System.out.println("save running....");
}
}
3.创建切面类
public class MyAspect {
public void before(){
System.out.println("前置增强...");
}
public void afterReturning(){
System.out.println("后置增强...");
}
//ProceedingJoinPoint://切入点
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前增强...");
//切点方法,spring封装好的
Object proceed = point.proceed();
System.out.println("环绕后增强...");
return proceed;
}
}
4.将目标和切面类的对象创建权交给spring
<?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"
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">
<!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
</beans>
5.在applicationContext.xml中配置织入关系
<?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"
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">
<!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<aop:before method="before" pointcut="execution(public void com.hao.aop.Target.save())"></aop:before>
<aop:after method="afterReturning" pointcut="execution(public void com.hao.aop.Target.save())"></aop:after>
<aop:around method="around" pointcut="execution(public void com.hao.aop.Target.save())"></aop:around>
</aop:aspect>
</aop:config>
</beans>
结果:
切点表达式的抽取,为了方便以后更改
<?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"
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">
<!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<!-- 抽取切入点表达式-->
<aop:pointcut id="myPointcut" expression="execution(public void com.hao.aop.Target.save())"></aop:pointcut>
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
<aop:after method="afterReturning" pointcut-ref="myPointcut"></aop:after>
<aop:around method="around" pointcut-ref="myPointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>