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 0;
            }
        };
        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(1, 2);
    System.out.println("--->"+result);
    result=proxy.div(4, 2);
    System.out.println("--->"+result);
    
}
}

aop的优势是:使每个事物逻辑位于一个模块,代码不分散便于维护和升级,业务模块更加简洁只包含核心代码

相关知识点如下:

原文地址:https://www.cnblogs.com/dazhi151/p/12644080.html