Spring核心技术之AOP

来源:互联网 发布:运营商重复放号 知乎 编辑:程序博客网 时间:2024/05/18 00:08

一:AOP简介:<1> Aop就是一个面向切面的编程  <2>本质过滤器

二:代理有两种:1.静态代理:比如明星与经纪人的关系

                2.动态代理:<1>让代理类实现InvocationHandler接口(反射包)

                           <2>重写方法Invoke():return method.invoke(原对象)

                           <3>混合产生代理对象:Proxy.newProxyinstance(原对象的类加载器,原对象的所有实现接口,原对象代理)

                           <4>调用接口中的方法:混合产生的代理对象,只能是实现对象的接口,不能是该对象

三:它们的专业术语:<1>原对象:target目标  <2>代理类:advice通知  <3>混合后产生的对象:Proxy代理

四:Spring中实现AOP

 1.target目标 + advice通知= proxy代理

 2.target目标 

 3.advice:实现Methodinterceptor(org.aopalliance.intercept.Methodinterceptor)接口

  4.实现方法:Invoke(methodinvocation.proceed();)

  5.Spring配置:<1>配置target  <2> 配置advice  <3>配置混合代理对象:ProxyFactoryBean全限定名:1.target目标 2.proxyinterfaces:接口的全限定名 3.interceptorNames:advice类的id

五:下面进行代码模式

   首先创建一个包com.prong.proxySpring,在创建一个类Artor,代码如下:

 package com.prong.proxySpring;

 public interface Artor {

    public void dance();


    }


创建一个前置通知类Yangyang,代码如下:

package com.prong.proxySpring;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class Yangyang implements MethodBeforeAdvice {
// 通知:前置通知
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("琳琳,准备一下,等下唱歌");
}
}


创建一个target目标类Xiuxiu,代码如下: 

package com.prong.proxySpring;


public class Xiuxiu implements Artor {

// 目标 target
public void dance() {
System.out.println("琳琳正在唱歌.....");
}
}


创建一个后置通知Yangy,代码如下:

package com.prong.proxySpring;

 import java.lang.reflect.Method;

 import org.springframework.aop.AfterReturningAdvice;

public class Yangy implements AfterReturningAdvice {
// 后置通知
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("琳琳,辛苦了,过来吃饭领工资了");
}
}


在/项目名/src/main/resources下创建一个配置文件名:applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义目标类target 琳琳 -->
<bean id="Xiuxiu" class="com.zking.proxySpring.Xiuxiu"></bean>
<!-- 定义通知类Advince 赵丽颖 -->
<bean id="Zhaoliy" class="com.zking.proxySpring.Zhaoliy"></bean>
<bean id="Yangyang" class="com.zking.proxySpring.Yangyang"></bean>
<bean id="Yangy" class="com.zking.proxySpring.Yangy"></bean>

<!-- 定义混合代理对象 proxy1 -->
<bean id="myProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 引用目标 -->
<property name="target" ref="Xiuxiu"></property>
<!-- 目标实现的所有接口 -->
<property name="proxyInterfaces">
<list>
<value>com.zking.proxySpring.Artor</value>
</list>
</property>
<!-- 引用通知 -->
<property name="interceptorNames">
<list>
<idref bean="Zhaoliy" />
<idref bean="Yangyang" />
<idref bean="Yangy" />
</list>
</property>
  </bean>
  </beans>


在创建好的包com.prong.proxySpring里面在创建一个测试类,代码以及运行结果如下:

package com.zking.proxySpring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
// 测试
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Artor artor = (Artor) ac.getBean("myProxy");
artor.dance();
}
}



原创粉丝点击