这里主要学习的是关于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. activiti工作流解决历史批注中文乱码

    /** * 根据流程实例查询流程的批注信息 * * @param processInstanceId * @return */ private List<Comment> findComm ...

  2. 深入理解Java内存模型(摘)

    --摘自 周志明<深入理解Java虚拟机> 转自 https://www.jianshu.com/p/15106e9c4bf3 深入理解Java内存模型(摘) java内存模型(Java ...

  3. 在vscode中怎样debug调试go程序

    随着互联网时代的飞速发展,我们编码使用的开发利器也在不断更新换代,古话说工欲善其事必先利其器,对于Java开发者而言,eclipse和idea这两款神器各有千秋,因自己的爱好可以选取不同的IDE,但是 ...

  4. 最全ElasticSearch6.5白金版本从构建ELK、集群搭建到和Mybatis结合操作详细讲解

    1.安装ElasticSearch6.5.1 解压相关的ElasticSearch6.5.1的tar包到目录下,如果我们需要使用JDBC来连接的话是需要到白金版以上的,以下为将基础版破解为白金版的方法 ...

  5. Redis启动服务和String常用命令

    Redis启动服务和String常用命令 1. 启动Redis服务 E:\redis>redis-server.exe redis.windows.conf _._ _.-``__ ''-._ ...

  6. css第二波

    目录 css第二波 盒子模型 浮动 三种取值 清除浮动 浮动页面布局 溢出 定位 相对定位 relative(相对定位) 绝对定位 absolute(绝对定位) 固定定位 fixed(固定) 模糊框 ...

  7. Java 入门学习知识点整理

    [JAVA一个文件写多个类 ( 同级类 ) 规则和注意点] 在一个.java文件中可以有多个同级类,  其修饰符只可以public/abstract/final/和无修饰符 public修饰的只能有一 ...

  8. MATLAB 颜色图函数(imagesc/scatter/polarPcolor/pcolor)

    2维的热度图 imagesc imagesc(x, y, z),x和y分别是横纵坐标,z为值,表示颜色 imagesc(theta,phi,slc); colorbar xlabel(); ylabe ...

  9. [阿里云-机器学习PAI快速入门与业务实战 ]课时1-机器学习背景知识以及业务架构介绍

    什么是机器学习? 机器学习指的是机器通过统计学算法,对大量的历史数据进行学习从而生成经验模型,利用经验模型指导业务. 目前机器学习主要在一下一些方面发挥作用: 营销类场景:商品推荐.用户群体画像.广告 ...

  10. 【转】.strip().split('t')和.strip().split()

    https://blog.csdn.net/qq_35290785/article/details/94780620 .strip().split('t')line =' nihao, zhenhao ...