[原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
问题引入

1.实现
原始实现方式:
package com.jason.spring.aop.helloworld;
public interface ArithmeticCaculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
ArithmeticCaculator
package com.jason.spring.aop.helloworld;
public class fArithmeticCaculatorloggingImpl implements ArithmeticCaculator {
@Override
public int add(int i, int j) {
System.out.println("the method add begins with [" + i + ", " + j + "]");
int result = i + j;
System.out.println("the method add end with " + result);
return result;
}
@Override
public int sub(int i, int j) {
System.out.println("the method sub begins with [" + i + ", " + j + "]");
int result = i - j;
System.out.println("the method sub end with " + result);
return result;
}
@Override
public int mul(int i, int j) {
System.out.println("the method mul begins with [" + i + ", " + j + "]");
int result = i * j;
System.out.println("the method mul end with " + result);
return result;
}
@Override
public int div(int i, int j) {
System.out.println("the method div begins with [" + i + ", " + j + "]");
int result = i / j;
System.out.println("the method div end with " + result);
return result;
}
}
fArithmeticCaculatorloggingImpl
问题:
1).代码混乱:越来越多的非业务需求(日志和验证等)加入后, 原有的业务方法急剧膨胀. 每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点.
2).代码分散: 以日志需求为例, 只是为了满足这个单一需求, 就不得不在多个模块(方法)里多次重复相同的日志代码. 如果日志需求发生变化, 必须修改所有模块.
解决方式:动态代理
思想:代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.

2. 手动通过动态代理实现(实际开发中不推荐,因为难度较高)

package com.jason.spring.aop.helloworld; import java.lang.reflect.Method;
import java.util.Arrays; import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy; public class ArithmeticCaculatorLoggingProxy { //要代理的对象
private ArithmeticCaculator target; public ArithmeticCaculatorLoggingProxy(ArithmeticCaculator target) {
this.target = target;
} public ArithmeticCaculator getLoggingProxy(){
ArithmeticCaculator proxy = null; //代理对象由哪一个类加载器负责加载
ClassLoader loader = target.getClass().getClassLoader(); //代理对象的类型,即其中有那些方法
Class[] interfaces = new Class[]{ArithmeticCaculator.class}; //当调用代理对象,其中的方法时,该执行的代码
InvocationHandler invocationHandler = new InvocationHandler() {
/**
* proxy:正在返回的那个代理对象,一般情况下,在invoke 方法中都不使用对象
* method:正在被调用的方法
* args:调用方法时,传入的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName(); //日志
System.out.println("The method " + methodName + "begins with " + Arrays.asList(args));
//执行方法
Object result = method.invoke(target, args);
//日志
System.out.println("The method " + methodName + "end with " + result);
return result;
}
}; proxy =(ArithmeticCaculator) Proxy.newProxyInstance(loader, interfaces, invocationHandler); return proxy;
} }
测试
Main.java
package com.jason.spring.aop.helloworld;
public class Main {
public static void main(String[] args) {
/*ArithmeticCaculator arithmeticCaculator = null;
arithmeticCaculator = new ArithmeticCaculatorImpl();*/
ArithmeticCaculator target = new ArithmeticCaculatorImpl();
ArithmeticCaculator proxy = new ArithmeticCaculatorLoggingProxy(target).getLoggingProxy();
int result = proxy.add(1, 2);
System.out.println("--> " + result);
result = proxy.sub(1, 2);
System.out.println("--> " + result);
}
}
3.简便方式:使用springAOP 原理 见下一节 105
package com.jason.spring.aop.impl;
public interface ArithmeticCaculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
ArithmeticCaculator
package com.jason.spring.aop.impl; import org.springframework.stereotype.Component; @Component
public class ArithmeticCaculatorImpl implements ArithmeticCaculator { @Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
} }
ArithmeticCaculatorImpl
package com.jason.spring.aop.impl; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面
//1.需要将该类放入到IOC 容器中
@Component
//2.再声明为一个切面
@Aspect
public class LoggingAspect { //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
//作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
//支持通配符
//@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
@Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins " + args);
} @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
public void fterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " end " + args);
} }
LoggingAspect
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.jason.spring.aop.impl"></context:component-scan> <!-- 使 AspjectJ 注解作用:自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
aop.xml
package com.jason.spring.aop.impl; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建Spring 的IOC 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml"); //2.从IOC 容器中获取 bean实例
ArithmeticCaculator arithmeticCaculator = (ArithmeticCaculator) ctx.getBean(ArithmeticCaculator.class); //3.使用bean
int result = arithmeticCaculator.add(1, 2);
System.out.println(result); result = arithmeticCaculator.div(1, 2);
System.out.println(result); } }
Main
[原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理的更多相关文章
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- 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> 图片参考了 ...
- Linux内核分析第七周学习笔记——Linux内核如何装载和启动一个可执行程序
Linux内核分析第七周学习笔记--Linux内核如何装载和启动一个可执行程序 zl + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study. ...
- 学习笔记:CentOS7学习之二十:shell脚本的基础
目录 学习笔记:CentOS7学习之二十:shell脚本的基础 20.1 shell 基本语法 20.1.1 什么是shell? 20.1.2 编程语言分类 20.1.3 什么是shell脚本 20. ...
随机推荐
- js中的call和apply
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:赵望野链接:https://www.zhihu.com/question/20289071/answer/14745394来源 ...
- jquery:closest和parents的主要区别
closest和parents的主要区别是:1,前者从当前元素开始匹配寻找,后者从父元素开始匹配寻找:2,前者逐级向上查找,直到发现匹配的元素后就停止了,后者一直向上查找直到根元素,然后把这些元素放进 ...
- HDU5942 : Just a Math Problem
\[\begin{eqnarray*}ans&=&\sum_{i=1}^ng(i)\\&=&\sum_{i=1}^n\sum_{d|i}\mu^2(d)\\&= ...
- Swagger - 前后端分离后的契约
前后端分离 按照现在的趋势,前后端分离几乎已经是业界对开发和部署方式所达成的一种共识.所谓的前后端分离,并不是传统行业中的按部门划分,一部分人只做前端(HTML/CSS/JavaScript等等),另 ...
- BestCoder Round #89 02单调队列优化dp
1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01 HDU 5944 水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...
- crawler4j 学习(二)
crawler4j 学习(二) 实现控制器类以制定抓取的种子(seed).中间数据存储的文件夹.并发线程的数目: public class Controller { public static voi ...
- Linux install SMplayer
sudo apt-add-repository ppa:rvm/smplayer sudo apt-get update sudo apt-get install smplayer smplayer- ...
- 【Leetcode】Longest Palindromic Substring
问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...
- Windows2003远程桌面单会话登录
在使用远程桌面连接到Windows2003的时候默认设置是同一用户可以进行多会话登录. (在winxp.win7及以后版本的windows中已经变成单会话登录.) 同用户多会话登录在管理上带来诸多麻烦 ...
- web自动化测试中绕开验证码登陆的方式
web自动化测试中登陆需验证码是很大的一个困扰.现推荐一种简单的避开验证码登陆的方式,先代码进入登录页,人工输入验证码登录后浏览器自动保存cookie,再在新的标签中登录. 具体代码如下: publi ...