这里主要学习的是关于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. 事务特性,事务的隔离级别以及spring中定义的事务传播行为

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  2. VWware Workstation 安装CentOS系统

    VWware Workstation 安装CentOS系统 下载CentOS系统镜像: 官方地址:https://www.centos.org/download/mirrors/ 在此可选择华为云服务 ...

  3. 文件上传transferTo一行代码的bug

    本次的项目环境为 Running with Spring Boot v1.5.10.RELEASE, Spring v4.3.14.RELEASE, 服务器环境为CentOS7.0. transfer ...

  4. Delphi10.3的DBGrid中memo类型显示内容而不是(WIDEMEMO)

    1]连接好数据库,并显示: 2]增加所有字段: 3]添加事件: //    FDQuery1UserName: TWideMemoField; procedure TForm1.FDQuery1Use ...

  5. 批量redis未授权检测工具&批量redis弱口令爆破工具

    今天需要然后就百度搜索了一波,然后自己稍微改了一下: #!/usr/bin/python3 # -*- coding: utf-8 -*- """ @Author: 偷来 ...

  6. asp.net net::ERR_ABORTED 500 (Internal Server Error) 无法加载JS CSS等文件的解决方法

    网站换服务器,部署上去后打开首页,js .css等文件始终无法加载,经过排查,问题出现在web.config文件中. <defaultDocument> <files> < ...

  7. P2341 [USACO03FALL][HAOI2006]受欢迎的牛 G 题解

    原题链接 POJ的链接 简要题意: 给定一张图,求多少个点,每个点都能到达它. 本题作为强连通分量的入门题. 何为强连通分量?有什么用? 下面一一解释. 首先,我们要确认,这道题目如果不用强连通分量而 ...

  8. Leetcode_面试题62. 圆圈中最后剩下的数字(约瑟夫环)

    经典的约瑟夫环,n个人排成一圈,第m个出队. 递归 code1 class Solution { public: int f(int n,int m){ if(n==1){ //递归边界,最后一个 r ...

  9. ICLR 2020 | 抛开卷积,multi-head self-attention能够表达任何卷积操作

    近年来很多研究将nlp中的attention机制融入到视觉的研究中,得到很不错的结果,于是,论文侧重于从理论和实验去验证self-attention可以代替卷积网络独立进行类似卷积的操作,给self- ...

  10. leetcode 945. 使数组唯一的最小增量

    题目 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1. 返回使 A 中的每个值都是唯一的最少操作次数. 示例 1: 输入:[1,2,2] 输出:1 解释:经过一次 mov ...