1.定义目标接口
public interface HelloWorldService {
void sayHello();
}
2.目标接口实现
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public void sayHello() {
System.out.println("hello world aop!");
}
}
3.定义切面支持类
有了目标类,该定义切面了,切面就是通知和切入点的组合,而切面是通过配置方式定义的,因此这定义切面前,我们需要定义切面支持类,切面支持类提供了通知实现
public class HelloWorldAspect {
/**
* @Description: 前置通知
* @Param: []
* @return: void
* @Author: anjunshuang
* @Date: 2019/8/27 14:00
*/
public void beforeAdvice(){
System.out.println("===========before advice");
}
/**
* @Description: 后置通知
* @Param: []
* @return: void
* @Author: anjunshuang
* @Date: 2019/8/27 14:07
*/
public void afterFinallyAdvice() {
System.out.println("===========after finally advice");
}
}
此处HelloWorldAspect类不是真正的切面实现,只是定义了通知实现的类,在此我们可以把它看作就是缺少了切入点的切面。
注:对于AOP相关类最后专门放到一个包下,如“aop”包,因为AOP是动态织入的,所以如果某个目标类被AOP拦截了并应用了通知,可能很难发现这个通知实现在哪个包里,因此推荐使用规约命名,方便以后维护人员查找相应的AOP实现。
4.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">
<!--0.配置aop命名空间 上面已配置-->
<!--1.配置目标类,就是你打算要切的类,一点都不圆润,切它-->
<bean id="helloWorldService" class="com.anjunshuang.aop.service.impl.HelloWorldServiceImpl"></bean>
<!--2.配置切面类,就是你打算用这个类去切它-->
<bean id="aspectHello" class="com.anjunshuang.aop.aspect.HelloWorldAspect"></bean>
<!--3.配置切面,在这个面上写明,你打算怎么切它-->
<aop:config>
<aop:pointcut id="pcHello" expression="execution(* com.anjunshuang.aop..*.*(..))"/>
<aop:aspect>
<aop:before method="beforeAdvice" pointcut-ref="pcHello"/>
<aop:after method="afterAdvice" pointcut="execution(* com.anjunshuang.aop..*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
AOP中execution解释
AOP中execution解释:例如 expression="execution(* com.anjunshuang.aop.service.impl..*.*(..))" 符号 含义 executioin() 表达式的主体 第一个* 表示返回值的类型 com.anjunshuang.aop.service.impl aop所切的服务的包名,即业务部分 包名后面的.. 表示当前包及子包 第二个”*“ 表示类名,*即所有类。此处可以自定义 .*(..) 表示任何方法名,括号表示参数,两个点表示任何参数类型
切入点使用<aop:config>标签下的<aop:pointcut>配置,expression属性用于定义切入点模式,默认是AspectJ语法,“execution(* com.anjunshuang.aop.service.impl..*.*(..))”表示匹配com.anjunshuang.aop.service.impl包及子包下的任何方法执行。
切面使用<aop:config>标签下的<aop:aspect>标签配置,其中“ref”用来引用切面支持类的方法。
前置通知使用<aop:aspect>标签下的<aop:before>标签来定义,pointcut-ref属性用于引用切入点Bean,而method用来引用切面通知实现类中的方法,该方法就是通知实现,即在目标类方法执行之前调用的方法。
最终通知使用<aop:aspect>标签下的<aop:after >标签来定义,切入点除了使用pointcut-ref属性来引用已经存在的切入点,也可以使用pointcut属性来定义,如pointcut="execution(* com.anjunshuang.aop.service.impl..*.*(..))",method属性同样是指定通知实现,即在目标类方法执行之后调用的方法。
5.代码测试
public class AspectTest {
@Test
public void testAspect(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/aspect/helloworld.xml");
HelloWorldService helloworldService = ctx.getBean("helloWorldService", HelloWorldService.class);
helloworldService.sayHello();
}
}