Spring 源码学习(4) —— 动态AOP使用示例
在实际工作中, 此bean可能是满足业务需要的核心逻辑, 例如test()方法中可能会封装着某个核心业务, 如果在test()方法前后加入日志来跟踪调试, 直接修改源码并不符合面向对象的设计模式, 而随意改动源码也会造成一定的风险。不用怕, Spring为此提供了解决方案。
1.创建用于拦截的bean
/**
* @filename: AopBean.java
* @desc AopBean 测试类
* @author: Wang Chinda
* @blog http://www.cnblogs.com/goodcheap
* @date: 2018-05-25 11:32
* @version: v1.0
* @copyright: Copyright © 2018 ༄ྂ祸ྂྂ害ོ༘苍ྂྂ生ོ༘࿐ྂ 版权所有
* @modify_history: -
* 20180525 Wang Chinda create
* 20180525 Wang Chinda modify method()
*/
package com.itdoc.learn.source.aop.demo; /**
* @desc AopBean 测试类
* @author Wang Chinda
* @create 2018-05-25 11:32
*/
public class AopBean { public void testMethod() {
System.out.println("this is aop test method!");
}
}
2.创建Advisor
Spring中摒弃了最原始的繁杂配置方式而采用@Aspect注解对POJO进行标注, 使AOP的工作大大简化, 但是要注意, 使用@Aspect注解需要导入第三方依赖aspectjweaver。
/**
* @filename: AspectJTest.java
* @desc aop切面控制
* @author: Wang Chinda
* @blog http://www.cnblogs.com/goodcheap
* @date: 2018-05-25 11:33
* @version: v1.0
* @copyright: Copyright © 2018 ༄ྂ祸ྂྂ害ོ༘苍ྂྂ生ོ༘࿐ྂ 版权所有
* @modify_history: -
* 20180525 Wang Chinda create
* 20180525 Wang Chinda modify method()
*/
package com.itdoc.learn.source.aop.demo; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; /**
* @desc aop切面控制, 创建Advisor
* @author Wang Chinda
* @create 2018-05-25 11:33
*/
@Aspect
public class AspectJTest { @Pointcut("execution(* *.*(..))")
public void test() {
} @Before("test()")
public void beforeTest() {
System.out.println("beforeTest");
} @After("test()")
public void afterTest() {
System.out.println("afterTest");
} @Around("test()")
public Object aroundTest(ProceedingJoinPoint point) {
Object obj = null;
try {
System.out.println("前置通知");
obj = point.proceed();
System.out.println("返回通知");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("异常通知");
} finally {
System.out.println("后置通知");
}
return obj;
} }
3.引入第三方依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itdoc.learn.source</groupId>
<artifactId>spring-01</artifactId>
<version>1.0-SNAPSHOT</version> <name>spring-01</name> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.3.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<!-- aop功能 @Aspect 注解依赖包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<!--<dependency>-->
<!--<groupId>aopalliance</groupId>-->
<!--<artifactId>aopalliance</artifactId>-->
<!--<version>1.0</version>-->
<!--</dependency>--> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<!--<dependency>-->
<!--<groupId>org.aspectj</groupId>-->
<!--<artifactId>aspectjrt</artifactId>-->
<!--<version>1.8.9</version>-->
<!--</dependency>--> <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4.配置aop:在xml中开启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: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"> <!-- 使 AspectJ 注解起作用, 自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy/> <bean id="test" class="com.itdoc.learn.source.aop.demo.AopBean"/>
<bean class="com.itdoc.learn.source.aop.demo.AspectJTest"/>
</beans>
5.测试
/**
* @filename: AopDemoClient.java
* @desc
* @author: Wang Chinda
* @blog http://www.cnblogs.com/goodcheap
* @date: 2018-05-25 12:00
* @version: v1.0
* @copyright: Copyright © 2018 ༄ྂ祸ྂྂ害ོ༘苍ྂྂ生ོ༘࿐ྂ 版权所有
* @modify_history: -
* 20180525 Wang Chinda create
* 20180525 Wang Chinda modify method()
*/
package com.itdoc.learn.source.aop.demo; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @desc
* @author Wang Chinda
* @create 2018-05-25 12:00
*/
public class AopDemoClient {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("test/aopDemo.xml");
AopBean aopBean = (AopBean) app.getBean("test");
aopBean.testMethod();
}
}
控制台输出:
五月 25, 2018 3:07:21 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e67b872: startup date [Fri May 25 15:07:21 CST 2018]; root of context hierarchy
五月 25, 2018 3:07:21 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [test/aopDemo.xml]
五月 25, 2018 3:07:22 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5c30a9b0: defining beans [org.springframework.aop.config.internalAutoProxyCreator,test,com.itdoc.learn.source.aop.demo.AspectJTest#0]; root of factory hierarchy
前置通知
beforeTest
this is aop test method!
返回通知
后置通知
afterTest
GitHub源码:https://github.com/wcd19901010/spring-01
Spring 源码学习(4) —— 动态AOP使用示例的更多相关文章
- spring源码学习之路---AOP初探(六)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 最近工作很忙,但当初打算学习 ...
- spring源码学习之路---深入AOP(终)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...
- Spring 源码学习——Aop
Spring 源码学习--Aop 什么是 AOP 以下是百度百科的解释:AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程通过预编译的方式和运行期动态代理实 ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点
Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...
- spring源码学习之路---IOC初探(二)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...
- Spring源码学习
Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
随机推荐
- python socket详解
Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...
- 团队Alpha冲刺(九)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- oracle 不能是用变量来作为列名和表名 ,但使用动态sql可以;
ORACLE 不能使用变量来作为列名 和表名 一下是个人的一些验证: DECLARE ename1 emp.ename%TYPE ; TYPE index_emp_type ) INDEX BY PL ...
- gridview 第一行编辑
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.c ...
- 使用salt-cloud创建虚拟机
salt-cloud也是基于openstack来做的,它可以支持多种云的使用.比如:Aliyun.Azure.DigitalOcean.EC2.Google Compute Engine.HP Clo ...
- java面试及答案
优秀总结博客 mybatis总结 java并发包相关 一.Java基础 1.String类为什么是final的. 2.HashMap的源码,实现原理,底层结构. hashmap3.反射中,Class. ...
- lucence学习系列之一 基本概念
1. Lucence基本概念 Lucence是一个java编写的全文检索类库,使用它可以为一个应用或者站点增加检索功能. 它通过增加内容到一个全文索引来完成检索功能.然后允许你基于这个索引去查询,返回 ...
- 【bzoj4709】[Jsoi2011]柠檬 斜率优化
题目描述 给你一个长度为 $n$ 的序列,将其分成若干段,每段选择一个数,获得 $这个数\times 它在这段出现次数的平方$ 的价值.求最大总价值. $n\le 10^5$ . 输入 第 1 行:一 ...
- MySQL一主两从
服务器说明: MySQL-Master:192.168.1. MySQL-Slave1:192.168.1. MySQL-Slave2:192.168.1. 关闭防火墙,关闭selinux 统一采用源 ...