5、AOP原理

aop编程

aop(aspect oriented programming)——面向切面(方面)编程,是对所有对象或者是一类对象编程,核心是(在增加代码的基础上,增加新功能)

汇编(伪机器指令)面向机器,c语言(面向过程)系统软件(操作系统,数据库,杀毒软件,防火墙…),java语法(面向对象->类-对象),面向切面 spring(->aop)面向n多对象编程

aop特别提醒:aop编程,实际上在开发框架本身用的很多,在实际项目中,用的不是很多。但是将来会越来越多,这是一个趋势

《5、AOP原理》

将事务、日志、、安全等公共功能抽取出来

aop原理+案例

aop编程说明,步骤:

1、定义接口
2、编写对象(被代理对象,又叫目标对象)
3、编写通知(前置通知,在目标对象方法调用前调用)
4、在beans.xml文件中进行配置
4.1、配置  被代理对象=目标对象
4.2、配置通知
4.3、配置代理对象 就是ProxyFactoryBean的对象实例
4.3.1、配置   代理接口集
4.3.2、织入通知
4.3.3、配置被代理对象

实际例子:

定义接口:

package com.cdtax.aop;

public interface TestServiceInter
{
	public void sayHello();
}

编写目标对象(被代理对象)

package com.cdtax.aop;

public class Test1Service implements TestServiceInter
{

	private String name;
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Override
	public void sayHello()
	{
		System.out.println("hello " + name);
	}

}

注意,目标对象要实现接口

编写通知(spring一共提供了五种通知类型,这里使用前置通知)

package com.cdtax.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodBeforeAdvice implements MethodBeforeAdvice
{

	/**
	 * method:被调用方法名字
	 * args:给method方法传递的参数
	 * target:目标对象
	 */
	public void before(Method method, Object[] args, Object target)
			throws Throwable
	{
		System.out.println("记录日志..." + method.getName());
	}

}

通知一定是实现了org.springframework.aop.MethodBeforeAdvice这个接口的

在beans.xml中进行配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置被代理的对象 -->
<bean id="test1Service" class="com.cdtax.aop.Test1Service">
	<property name="name" value="小明"></property>
</bean>

<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.cdtax.aop.MyMethodBeforeAdvice">
</bean>

<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> 
<!-- 代理接口集 -->
	<property name="proxyInterfaces">
		<list>
			<value>com.cdtax.aop.TestServiceInter</value><!-- 注意要写接口全称 -->
		</list>
	</property>
	
	<!-- 把通知织入到代理对象 -->
	<property name="interceptorNames">
		<!-- 相当于把 MyMethodBeforeAdvice前置通知和代理对象关联起来,我们也可以把通知看成拦截器,struts2核心就是拦截器 -->
		<value>myMethodBeforeAdvice</value>
	</property>
	
	<!-- 配置被代理对象,可以指定 -->
	<property name="target" ref="test1Service">
		
	</property>
</bean>
</beans>

注意的是配置代理对象的各个属性,属性名是应经给定的,不是随意起名的,如<property name=”proxyInterfaces”>的proxyInterfaces

编写测试类:

package com.cdtax.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App1
{
	public static void main(String[] args)
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/cdtax/aop/beans.xml");
		
// Test1Service ts = (Test1Service) ac.getBean("test1Service"); // ts.sayHello();
		
		TestServiceInter ts1 =  (TestServiceInter) ac.getBean("proxyFactoryBean");
		ts1.sayHello();
	}
}

要注意,不能像红色注释那样调用被代理对象,而是应该调用代理对象

运行结果:

记录日志…sayHello
hello 小明
前置通知在被代理对象(目标对象)的方法调用前被调用了
假设我们还有一个接口:

package com.cdtax.aop;

public interface TestServiceInter2
{
	public void sayBye();
}

被代理对象(目标对象)同时实现了两个接口:

package com.cdtax.aop;

public class Test1Service implements TestServiceInter,TestServiceInter2
{

	private String name;
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Override
	public void sayHello()
	{
		System.out.println("hai " + name);
	}

	@Override
	public void sayBye()
	{
		System.out.println("bye " + name);
	}

}

这时只要在beans.xml中配置相应的接口就可以了:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置被代理的对象 -->
<bean id="test1Service" class="com.cdtax.aop.Test1Service">
	<property name="name" value="小明"></property>
</bean>

<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.cdtax.aop.MyMethodBeforeAdvice">
</bean>

<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> 
<!-- 代理接口集 -->
	<property name="proxyInterfaces">
		<list>
			<value>com.cdtax.aop.TestServiceInter</value>
			<value>com.cdtax.aop.TestServiceInter2</value>
		</list>
	</property>
	
	<!-- 把通知织入到代理对象 -->
	<property name="interceptorNames">
		<!-- 相当于把 MyMethodBeforeAdvice前置通知和代理对象关联起来,我们也可以把通知看成拦截器,struts2核心就是拦截器 -->
		<value>myMethodBeforeAdvice</value>
	</property>
	
	<!-- 配置被代理对象,可以指定 -->
	<property name="target" ref="test1Service">
		
	</property>
</bean>
</beans>

修改一下测试类,调用接口2的方法:

package com.cdtax.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App1
{
	public static void main(String[] args)
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/cdtax/aop/beans.xml");
		
//		Test1Service ts = (Test1Service) ac.getBean("test1Service");
//		ts.sayHello();
		
		TestServiceInter ts1 =  (TestServiceInter) ac.getBean("proxyFactoryBean");
		ts1.sayHello();
		((TestServiceInter2)ts1).sayBye();
	}
}

同时实现了几个接口的类,可以在几个接口类型之间随意转换,运行结果:

记录日志…sayHello
hai 小明
记录日志…sayBye
bye 小明

aop的原理图:

《5、AOP原理》

对照上面的例子和原理图,介绍一下术语:

(1)切面(aspect):要实现的交叉功能,是系统模块化的一个切面或领域,如日志记录

(2)连接点:应用程序执行过程中插入切面的地点,可以是方法调用,异常抛出,或者要修改的字段。

(3)通知:切面的实际实现,他通知系统新的行为。如在日志通知包含了实现日志功能的代码,如向日志文件写日志。通知在连接点插入到应用系统中。

(4)切入点:定义了通知应该应用在哪些连接点,通知可以应用到AOP框架支持的任何连接点。

(5)引入:为类添加新方法和属性。

(6)目标对象:被通知的对象。既可以是你编写的类也可以是第三方类。

(7)代理:将通知应用到目标对象后创建的对象,应用系统的其他部分不用为了支持代理对象而改变。

(8)织入:将切面应用到目标对象从而创建一个新代理对象的过程。织入发生在目标对象生命周期的多个点上:
        编译期:切面在目标对象编译时织入,这需要一个特殊的编译器
        类装载期:切面在目标对象被载入JVM时织入,这需要一个特殊的类载入器
        运行期:切面在应用系统运行时织入

    原文作者:AOP
    原文地址: https://blog.csdn.net/kaoa000/article/details/18838997
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞