这里主要学习的是关于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. 自动控制理论的MATLAB仿真实例(二)

    %求方程的解 x=sym('x'); fx=(3*x*x+2*x)*(x*x+2.32*x+4)-(2*x+2.32)*(x*x*x+x*x) fx = 

  2. vue缓存当前路由(在输入框中输入信息后,跳转其他路由再回来,仍可看到刚刚输入的内容等)

    缓存路由页面的当前状态:   <transition name="fade" mode="out-in"> <keep-alive> & ...

  3. Mol Cell Proteomics. | Identification of salivary biomarkers for oral cancer detection with untargeted and targeted quantitative proteomics approaches (解读人:卜繁宇)

    文献名:Identification of salivary biomarkers for oral cancer detection with untargeted and targeted qua ...

  4. java web 获取 网页访问次数

    ServletContext context = request.getServletContext(); /** * 从ServletContext中获取计数器对象 */Integer count ...

  5. Mysql5.7及以上版本 ONLY_FULL_GROUP_BY报错

    近期在开发过程中,因为项目开发环境连接的mysql数据库是阿里云的数据库,而阿里云的数据库版本是5.6的.而测试环境的mysql是自己安装的5.7.因此在开发过程中有小伙伴不注意写了有关group b ...

  6. 利用Docker手动构建WebLogic镜像的步骤

    info 我的Docker环境信息如下: [root@localhost ~]# docker info -f " OSType: {{.OperatingSystem}} {{.Archi ...

  7. Python之open()函数

    Python内置了读写文件的函数open(). # 方法一 # 使用Python内置的open()函数,传入文件名和标示符 f = open('E:/test/driver.py', 'r', enc ...

  8. 201771010111-李瑞红 实验一 软件工程准备-<构建之法-现代软件工程-基础认识和理解>

    |||||||| | :--

  9. java-Deque

    2020-03-07 13:42:05 双端队列与通常的Queue的区别仅仅在于多了双端队列可以在队首队尾进行插入或者删除操作. 队尾添加:offerLast 队尾删除:pollLast 队尾查询:p ...

  10. leetcode签到 892. 三维形体的表面积

    题目 三维形体的表面积 在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上. 请你返回最 ...