Spring基础学习(四)—AOP

一、AOP基础

1.基本需求

    

需求: 日志功能,在程序执行期间记录发生的活动。

ArithmeticCalculate.java

public interface ArithmeticCalculate{

	public int add(int a,int b);

	public int sub(int a,int b);

	public int mul(int a,int b);

	public int div(int a,int b);
}

ArithmeticCalculateImpl.java

public class ArithmeticCalculateImpl implements ArithmeticCalculate{

	@Override
	public int add(int a,int b){
		System.out.println("The method add.....begin");
		int result = a + b;
		System.out.println("The method add.....end");
		return result;
	}

	@Override
	public int sub(int a,int b){
		System.out.println("The method sub.....begin");
		int result = a - b;
		System.out.println("The method sub.....end");
		return result;
	}

	@Override
	public int mul(int a,int b){
		System.out.println("The method mul.....begin");
		int result = a * b;
		System.out.println("The method mul.....end");
		return result;
	}

	@Override
	public int div(int a,int b){
		System.out.println("The method div.....begin");
		int result = a / b;
		System.out.println("The method div.....end");
		return result;
	}

}

以上这样写会出现两种问题。

(1)代码混乱

     越来越多的非业务需求加入后,原有的业务方法急剧膨胀。每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点。    

(2)代码分散

     以日志需求为例,只是为了满足这个单一需求,就不得不在多个模块重复相同的代码日志,如果日志需求发生改变还得修改所有的需求。

使用动态代理解决以上问题

原理: 使用一个代理将对象包装起来,然后改代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。

ArithmeticCalculateProxy.java

public class ArithmeticCalculateProxy{

	//要代理的对象
	private ArithmeticCalculate target;

	public ArithmeticCalculateProxy(){
	}

	public ArithmeticCalculateProxy(ArithmeticCalculate target){
		this.target = target;
	}

	public ArithmeticCalculate getProxy(){
		ArithmeticCalculate proxy = null;

		//代理对象由哪一个类加载器负责加载
		ClassLoader loader = target.getClass().getClassLoader();

		//代理对象的类型,即有哪些方法
		Class[] interfaces = new Class[]{ArithmeticCalculate.class};

		//当调用代理对象其中方法时,该执行的代码
		InvocationHandler handler = new InvocationHandler(){

			/*
			 * proxy: 正在返回的那个代理对象,一般情况下,在invoke方法中都不使用
			 * method: 正在被调用的方法
			 * args:调用方法时传入的参数
			 */
			@Override
			public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{

				String methodName = method.getName();

				//日志
				System.out.println("The method " + methodName +" begin......");
				//执行方法
				Object result = method.invoke(target,args);
				//日志
				System.out.println("The method " + methodName +" end......");
				return result;
			}
		};

		proxy = (ArithmeticCalculate)Proxy.newProxyInstance(loader,interfaces,handler);

		return proxy;
	}
}

Test.java

	@Test
	public void testCalculate(){
		ArithmeticCalculate target = new ArithmeticCalculateImpl();
		ArithmeticCalculate proxy = new ArithmeticCalculateProxy(target).getProxy();
		System.out.println(proxy.add(4,2));
		System.out.println(proxy.sub(4,2));
	}

结果:

      The method add begin......

      The method add end......

      6

      The method sub begin......

      The method sub end......

      2

时间: 2024-08-02 06:59:05

Spring基础学习(四)—AOP的相关文章

spring 基础学习笔记

spring 初始理解 1.spring 只是一个框架,是一些相应的 jar包,相当于一个项目的管理者,各种其他框架的中介,项目的监管机构. 2.主要特点是依赖注入DI.面向切面AOP,控制反转IOC 3.核心:加载applicationContext.Xml配置文件,生成applicationContext对象,在配置文件中的bean也会生成各种bean对象,这些bean对象相当于配置文件中的各个类的初始化,或者框架配置, spring通过applicationContext这个对象去管理这些

Spring基础学习,没有理论 都是干货

##学习需要掌握:反射和最简单的MVC架构流程 ##对spring有过基础性的理论知识 ##按照代码一步步来,30分钟掌握Spring的基本使用 1:先用spring创建一个最简单的对象 首先创建一个对象 package com.itmayiedu.entity; public class UserEntity { private String name; private Integer age; public String getName() { return name; } public v

Spring入门学习(四)

使用注解开发 在Spring4之后,要使用注解开发,必须要保证aop包的导入了,而且需要导入context约束,增加注解的支持! <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst

Spring基础学习笔记-Bean的基础知识

一. Bean的定义,初始化,使用和销毁 二.ref指定依赖的三种模式 三.Bean的五种自动装配模式(autowire) 四.Bean依赖检查的4种模式:配合atuowire使用,dependency-check="" 五.集合的注入方式 六.管理Bean config.xml文件<!--Bean的配置文档--><!--首先定义为XML的方式来存储Bean的配置--><?xml version="1.0" encoding="

使用JavaConfig方式-Spring 基础学习

在Spring的使用中,大量采用xml方式配置类之间的关系太过于繁琐(个人这么认为),而在学习了Spring4后发下使用JavaConfig方式来配置这些关系会更加的简单明了. 测试环境 1. Apache Maven 2. JDK 1.8 3. IDEA 15 先决条件 1. 使用IDEA创建一个Maven项目 2. 在pom.xml 中引用 <dependency> <groupId>org.springframework</groupId> <artifac

linux基础学习四

本次继续学习linux基础命令,包括stat.touch.cp.mv.rm.tree.mkdir.rmdir stat 命令格式 stat [OPTION]... FILE... 命令功能 显示文件的时间戳,即访问时间.修改时间和改变时间 stat [OPTION]... FILE...      [[email protected] ~]# stat newfile       File: `newfile'      Size: 0          Blocks: 0          I

spring再学习之AOP事务

spring中的事务 spring怎么操作事务的: 事务的转播行为: 事务代码转账操作如下: 接口: public interface AccountDao { //加钱 void addMoney(Integer id,Double money); //减钱 void decreaseMoney(Integer id,Double Money); } 实现类: import org.springframework.jdbc.core.support.JdbcDaoSupport; public

Spring框架学习06——AOP底层实现原理

在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflect.*包提供的方法,必须要借助一个接口才能产生代理对象,对于使用业务接口的类,Spring默认使用JDK动态代理实现AOP.代码示例如下:创建dao包,并创建StuDao接口和StuDaoImpl实现类,StuDao接口 public interface StuDao { public void a

Spring框架学习05——AOP相关术语详解

1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查.缓存).Spring AOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码. 2.AOP的相关术语 在Spring AOP 框架中涉及以下常用术语: 连接点(Joinpoint):是指程序运行中的一些时间点,即那些被拦截到的点,例如方法的调用或异常的抛