基本环境准备
导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
添加xml 新约束
在 spring 核心配置文件中添加 aop 约束
<?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:p="http://www.springframework.org/schema/p"
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">
</beans>
Spring的 五种通知类型
通知类型 | 标签 | 标签 |
---|
前置通知 | aop:before | 目标方法执行前增强 | 后置通知 | aop:after | 目标方法执行后增强 | 环绕通知 | aop:around | 目标方法执行前后都增强 | 返回通知 | aop:after-returning | 目标方法成功执行之后调用 | 异常通知 | aop:after-throwing | 在目标方法抛出异常后调用 |
XML 方式配置AOP
根据五种通知类型 配置增强类
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class SelectAdvice {
public void before(JoinPoint joinPoint){
System.out.println("前置");
System.out.println("目标类是: "+ joinPoint.getTarget());
System.out.println("被织入的目标类方法为: "+ joinPoint.getSignature().getName());
}
public void afterReturning(){
System.out.println("后置(方法不出现异常时调用! )");
}
public void around(ProceedingJoinPoint pjp) {
System.out.println("环绕前置");
try {
pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕后置");
}
public void exception(){
System.out.println("异常通知");
}
public void after(){
System.out.println("最终通知(类似于异常处理机制中的 finally)");
}
}
xml配置
<bean id="select" class="cn.Select"/>
<bean id="selectAdvice" class="cn.SelectAdvice"/>
<aop:config>
<aop:pointcut id="abc" expression="execution(* cn..*.*(..))"/>
<aop:pointcut id="def" expression="execution(* cn..*.update*(..))"/>
<aop:aspect ref="selectAdvice">
<aop:before method="before" pointcut-ref="abc"/>
<aop:after-returning method="afterReturning" pointcut-ref="abc"/>
<aop:around method="around" pointcut-ref="abc"/>
<aop:after-throwing method="exception" pointcut-ref="abc"/>
<aop:after method="after" pointcut-ref="abc"/>
</aop:aspect>
</aop:config>
测试
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("classpath:application_context.xml");
Select select =
(Select)app.getBean("select");
select.select();
}
基于注解方式配置 AOP
核心配置文件扫描目标类和增强类所在包
<context:component-scan base-package="cn.aop"/>
<aop:aspectj-autoproxy/>
目标类添加注解
@Component
增强类配置(切面)
切入点表达式
@Aspect
@Component
@Pointcut("execution(* cn..*.select(..))")
private void abc(){
}
@Pointcut("execution(* cn..*.update(..))")
private void def(){
}
其他注解
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SelectAdvice {
@Pointcut("execution(* cn..*.select(..))")
private void abc(){
}
@Pointcut("execution(* cn..*.update(..))")
private void def(){
}
@Before("abc()")
public void before(JoinPoint joinPoint){
System.out.println("前置");
System.out.println("目标类是: "+ joinPoint.getTarget());
System.out.println("被织入的目标类方法为: "+ joinPoint.getSignature().getName());
}
@AfterReturning("abc()")
public void afterReturning(){
System.out.println("后置(方法不出现异常时调用! )");
}
@Around("abc()")
public void around(ProceedingJoinPoint pjp) {
System.out.println("环绕前置");
try {
pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕后置");
}
@AfterThrowing("abc()")
public void exception(){
System.out.println("异常通知");
}
@After("abc()")
public void after(){
System.out.println("最终通知(类似于异常处理机制中的 finally)");
}
}
测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:application_context.xml"})
public class Test1 {
@Autowired
Select select;
@Test
public void aopTest(){
select.add();
}
}
|