spring08
这里主要学习的是关于spring之后中与ioc不同的aop技术;面向切面编程是spring基石之一;
解决代码混乱文体,代码分散,当部分修改时,要逐个修改当更多的日志以及验证介入之后会使代码变得更加的混乱不好维护。

使用aop之前的关于的相关操作(相对于比较复杂)
具体的代码如下:
package aop;
public interface AtithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
package aop;
public class AtithmeticCalculatorImpl implements AtithmeticCalculator {
@Override
public int add(int i, int j) {
// System.out.println("the method add begin>>>>>>>>");
// TODO Auto-generated method stub
int result=i+j;
// System.out.println("the method add end>>>>>>>");
return result;
}
@Override
public int sub(int i, int j) {
// TODO Auto-generated method stub
int result=i-j;
return result;
}
@Override
public int mul(int i, int j) {
// TODO Auto-generated method stub
int result=i*j;
return result;
}
@Override
public int div(int i, int j) {
// TODO Auto-generated method stub
int result=i/j;
return result;
}
}
package aop; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays; import org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler;
public class wenProxy {
//要代理的对象
private AtithmeticCalculator target;
public wenProxy(AtithmeticCalculator target)
{
this.target=target;
}
public AtithmeticCalculator getwenproxy()
{
AtithmeticCalculator proxy=null;
//代理对象由哪一个加载器负责加载
ClassLoader loader=target.getClass().getClassLoader();
//代理对象的类型,既其中的代理方法
Class [] interfaces=new Class[] {AtithmeticCalculator.class};
//当调用代理的对象其中的方法时候,要执行的代码
InvocationHandler h=new InvocationHandler() {
/**
* proxy:正在返回的哪个代理的对象,一般情况夏。在invoke方法中都不使用该对象
* method:正在被调用的方法
* args:调用方法时,传入参数*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
String methodname=method.getName();
//日志
System.out.println("the method "+methodname+" begin with" +Arrays.asList(args));
//执行方法
Object result=method.invoke(target, args);
//日志
System.out.println("the method "+methodname+" begin with" +result); return ;
}
};
proxy=(AtithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}
package aop;
public class spring {
public static void main (String []args) {
AtithmeticCalculator target=new AtithmeticCalculatorImpl();
AtithmeticCalculator proxy=new wenProxy(target).getwenproxy();
int result=proxy.add(, );
System.out.println("--->"+result);
result=proxy.div(, );
System.out.println("--->"+result);
}
}
aop的优势是:使每个事物逻辑位于一个模块,代码不分散便于维护和升级,业务模块更加简洁只包含核心代码
相关知识点如下:

spring08的更多相关文章
- spring08事务
实现的效果是: 用户在购买股票的时候,钱减少!股票增加! 模拟出现的问题是! 用户在购买股票的时候,钱减少! 股票没有增加! 01.创建对应的数据库 02.创建对应实体类 public class ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
- datalog
https://en.wikipedia.org/wiki/Datalog http://www.csd.uoc.gr/~hy562/1112_spring/instr_material/WhatYo ...
- Spring4学习回顾之路11-AOP
Srping的核心除了之前讲到的IOC/DI之外,还有一个AOP(Aspect Oriented Programming:面向切面编程):通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...
随机推荐
- QT使用信号量QSemaphore处理大量数据
实现如下:
- 【Weiss】【第03章】练习3.12:单链表倒置
[练习3.12] a.编写一个非递归过程以O(N)时间反转单链表. b.使用常数附加空间编写一个过程以O(N)时间反转单链表. Answer: 这题的b貌似没啥意义,在a小题里直接用头插法,不断地将头 ...
- 文件合并cat and paste
cat 纵向合并 cat file1 file 2 paset横向合并 wc用法 sort用法
- 神器cut基因剪
cut cut 不就是切嘛,没错就是它--我给他起了一个外号基因剪刀 来我们学一下怎么使用这个命令 cut --help [root@ESProbe ~]# cut --help Usage: cut ...
- Android 开发技术周报 Issue#273
新闻/News Android 11有新玩法:双击手机背部截屏/进入多任务界面 Android 11 DP2证实了类似AirDrop的附近文件分享功能 谷歌发布Camera Go:即使入门机也能有出色 ...
- JavaScript零宽字符
什么是零宽字符 一种不可打印的Unicode字符, 在浏览器等环境不可见, 但是真是存在, 获取字符串长度时也会占位置, 表示某一种控制功能的字符. 常见的零宽字符有哪些 零宽空格(zero-widt ...
- 14. LiveBos编号自动生成
(1) var temp="Apex"; var no=""+ABS_DYNSERIALNO(true,temp); var len=no.length; va ...
- 从零开始学习R语言(八)——R语言绘图
本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/74051739 也同步更新于我的个人博客:https://www.cnblogs.com/nickwu/p/125683 ...
- [极客大挑战 2019]BabySQL 1
考点就是一系列的sql注入操作 和 replace函数过滤 进入页面如图 基础过滤测试 union .select .information_schema试试有没有被过滤 ?username=ad ...
- eclipse、 IDEA中字母大小写转换快捷键
eclipse 中字母大小写切换快捷键: ctrl + shift + x 转为大写 ctrl + shift + y 转为小写 IDEA 中字母大小写切换快捷键: ctr + sh ...