这里主要学习的是关于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的更多相关文章

  1. spring08事务

    实现的效果是:  用户在购买股票的时候,钱减少!股票增加! 模拟出现的问题是!  用户在购买股票的时候,钱减少! 股票没有增加! 01.创建对应的数据库 02.创建对应实体类 public class ...

  2. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  3. datalog

    https://en.wikipedia.org/wiki/Datalog http://www.csd.uoc.gr/~hy562/1112_spring/instr_material/WhatYo ...

  4. Spring4学习回顾之路11-AOP

    Srping的核心除了之前讲到的IOC/DI之外,还有一个AOP(Aspect Oriented Programming:面向切面编程):通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...

随机推荐

  1. Win10下如何安装和搭建appium自动化测试环境

    转:https://www.cnblogs.com/huainanhai/p/11577419.html 安装Android SDK https://www.jianshu.com/p/2acdc1d ...

  2. 初识JVM:(二)Java的垃圾回收机制详解

    声明:本文主要参考https://www.cnblogs.com/codeobj/p/12021041.html 仅供个人学习.研究之用,请勿用于商业用途,如涉及侵权,请及时反馈,立刻删除. 一.Ja ...

  3. ssh 公钥 下载选择的时候 下拉选择 ssh 然后 git clone

    ssh 公钥 下载选择的时候 下拉选择 ssh 然后 git clone

  4. ant-design-pro 如何打包成 本地html,双击即可查看

    由于 ant-design-pro 的 mock 是一个单独的服务,所以没有办法整合到一起打包.暂时我是没有找到. 所以解决方案就是不用 mock . 由于 系统有异步调取,所以一旦有异步调取就会失败 ...

  5. Elasticsearch系列---多字段搜索

    概要 本篇介绍一下multi_match的best_fields.most_fields和cross_fields三种语法的场景和简单示例. 最佳字段 bool查询采取"more-match ...

  6. setTimeout和setImmediate到底谁先执行,本文让你彻底理解Event Loop

    笔者以前面试的时候经常遇到写一堆setTimeout,setImmediate来问哪个先执行.本文主要就是来讲这个问题的,但是不是简单的讲讲哪个先,哪个后.笼统的知道setImmediate比setT ...

  7. WordCount程序(Java)

    Github项目地址:https://github.com/softwareCQT/web_camp/tree/master/wordCount 一.题目描述 实现一个简单而完整的软件工具(源程序特征 ...

  8. 3. webdriver的常用方法

    WebDriver常用方法: clear(): 清除文本. send_keys (value): 模拟按键输入. click(): 单击元素. submit():用于提交表单 from seleniu ...

  9. [极客大挑战 2019]PHP1

    知识点:PHP序列化与反序列化,最下方有几个扩展可以看一下 他说备份了,就肯定扫目录,把源文件备份扫出来 dirsearch扫目录扫到www.zip压缩包

  10. C# Linq方式生成SAP对接的XML格式内容(一般处理程序 ashx )

    Linq生成XML的方法: string CreateXML(string strkey, string strDATAJSON) { XDeclaration dec = new XDeclarat ...