学习aop源码之前首先需要有环境,很多小伙伴可能会卡在这里,接下来咱们看看aop的配置
前提
已经安装了aspectj-1.9.3.jar,没有安装的参考上面的链接
gradle配置
到你测试工程下找到build.gradle文件
修改配置
找到文件的最下方,添加依赖
dependencies {
compile(project(":spring-beans"))
compile(project(":spring-core"))
compile(project(":spring-context"))
compile(project(":spring-webmvc"))
compile(project(":spring-jdbc"))
compile(project(":spring-orm"))
compile(project(":spring-tx"))
compile(project(":spring-web"))
compile(project(":spring-context-indexer"))
compile(project(":spring-context-support"))
compile(project(":spring-expression"))
compile(project(":spring-instrument"))
compile(project(":spring-jcl"))
compile(project(":spring-jms"))
compile(project(":spring-messaging"))
compile(project(":spring-oxm"))
compile(project(":spring-test"))
compile(project(":spring-webflux"))
compile(project(":spring-websocket"))
compile(project(":spring-aspects"))
compile("org.aspectj:aspectjweaver:1.9.6")
compile(project(":spring-aop"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
刷新工程
aop配置
咱们这里配置通过注解和xml两种
创建切面
//@Aspect
//@Component
public class LogAspect {
//切入点
// @Pointcut("execution(* com.msgqu.debug.aop.service.*.*(..))")
public void pointCut(){ }
// @Before("pointCut()")
public void before(JoinPoint joinPoint){
System.out.println("before.....");
}
// @Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint){
System.out.println("around.....before");
Object res;
try {
res = joinPoint.proceed();
}catch (Throwable throwable){
res = throwable.getMessage();
}
System.out.println("around.....after");
return res;
}
// @After("pointCut()")
public void after(JoinPoint joinPoint){
System.out.println("after.....");
}
// @AfterReturning(value = "pointCut()",returning = "result")
public void afterReturning(JoinPoint joinPoint,Object result){
System.out.println("afterReturning.....");
}
// @AfterThrowing(pointcut = "pointCut()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Exception e){
System.out.println("afterThrowing....." + e.getMessage());
}
}
创建实现类
//@Service
public class BookServiceImpl implements BookService {
public void get(int i){
System.out.println("获取书籍。。。。");
}
public void add(int i){
System.out.println("增加书籍。。。。" + i);
// if(true){
// throw new IllegalStateException("throwing");
// }
}
public void update(int i){
System.out.println("更新书籍。。。。");
}
public void delete(int i){
System.out.println("删除书籍。。。。");
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--如果使用注解,那么需要放开包扫描-->
<!-- <context:component-scan base-package="com.msgqu.debug.aop"/>-->
<!--开启自动代理-->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/>-->
<!-- <aop:aspectj-autoproxy/>-->
<!-- 配置bean -->
<bean id="bookServiceImpl" class="com.msgqu.debug.aop.service.BookServiceImpl"></bean>
<!-- 配置切面的bean -->
<bean id="logAspect" class="com.msgqu.debug.aop.log.LogAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(* com.msgqu.debug.aop.service.*.*(..))"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="logAspect">
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试
public class AopTest {
public static void main(String[] args) {
MyClassPathXmlApplicationContext ac = new MyClassPathXmlApplicationContext("aop.xml");
BookService bookService = ac.getBean(BookService.class);
bookService.add(1);
ac.close();
}
}
测试结果
before.....
around.....before
增加书籍。。。。1
around.....after
afterReturning.....
after.....
到这里咱们aop的配置已完成,咱们就可以愉快的阅读源码了~~~