• 自学spring AOP


    本人是一个编程新手也是第一次写博客 这篇文章是我结合网上的资料和一些书籍学的 如果有不对之处请留言告知 

    本文介绍了AOP的两个知识点

    1: 代理

    代理有两种 我先写:Java静态代理

    1:建立一个接口

    package com.proxy;
    /**
     * 
    * @ClassName: IPerson
    * @Description: 抽象的定义这个角色
    * @author 
    * @date 2017年10月24日 下午6:40:43
    *
     */
    public interface IPerson {
    	public void show();
    }
    

      

    2:创建类实现接口

    package com.proxy;

    public class Lishi implements IPerson {

    public void show() {
    System.out.println("啦啦啦");
    }

    }

      

    3:创建代理类实现接口(构造方法中要求传入接口作为参数)

    package com.proxy;

    /**

    * @ClassName: lishiproxy
    * @Description: 代理类
    * @author 
    * @date 2017年10月24日 下午6:41:33
    *
    */
    public class Lishiproxy implements IPerson {
    private IPerson ip;

    public lishiproxy(IPerson iPerson) {
    super();
    this.ip = iPerson;
    }

    public void show() {
    ip.show();
    }

    }

      

    4:测试类调用代理类进行测试

    package com.proxy;
    
    import org.junit.Test;
    
    public class Lishitest {
    	@Test
    	public void showls() {
    		
    
    		lishi ls = new lishi();
    		// 一个代理能够实现多个实现类
    		IPerson lip = new lishiproxy(ls);
    		lip.show();
    
    	}
    }
    

      

    2:Java动态代理

    先来步骤

    1:建立一个接口

    package com.proxy2;
    
    public interface Iperson2 {
    	public void eat();
    }
    

      

    2:创建类实现接口

    package com.proxy2;
    
    public class Ruan implements Iperson2 {
    
    	public void eat() {
    		System.out.println("啦啦啦啦啦");
    	}
    
    }
    

      

    3:创建代理类实现接口InvocationHandler(构造方法要求传入接口作为参数)

    package com.proxy2;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class proxy2 implements InvocationHandler {
    	private Iperson2 iperson2;
    
    	public proxy2(Iperson2 iperson2) {
    		super();
    		this.iperson2 = iperson2;
    	}
    
    	    // 执行
    	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    		// 传入的是执行的人
    		return method.invoke(iperson2, args);
    	}
    
    }
    

      

    注意重写一个方法

    4:测试实现代理类进行测试

    package com.proxy2;
    
    import java.lang.reflect.Proxy;
    
    import org.junit.Test;
    
    public class Ruantest {
    	@Test
    	public void eat() {
    		Iperson2 ip2 = new Ruan();
    		proxy2 proxy2 = new proxy2(ip2);
    
    		Iperson2 iperson2 = (Iperson2) Proxy.newProxyInstance(Ruan.class.getClassLoader(), Ruan.class.getInterfaces(),
    				proxy2);
    		iperson2.eat();
    
    	}
    }
    

      

    2:通知

    通知有三种1:环绕通知

                     2:前置通知

                     3:后置通知

    三种通知我写在一起了 一次性发出来把 难得分开了 步骤我还是会写出来

    1:建立一个接口

    package com.proxy3;
    
    public interface IPerson3 {
    	public void seelp();
    	public void cadd();
    }
    

      

    2:创建类实现接口    目标

    package com.proxy3;
    
    public class Siming implements IPerson3 {
    
    	public void seelp() {
    		System.out.println("嘻嘻嘻嘻嘻嘻.哈哈哈哈哈");
    	}
    
    	public void cadd() {
    System.out.println("哈哈哈,通知过滤");		
    	}
    
    }
    

      

    3:创建代理类  通知

    package com.proxy3;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    public class BeforeAdvice implements MethodBeforeAdvice {
    
    	public void before(Method method, Object[] args, Object target) throws Throwable {
    		System.out.println("前置的通知");
    	}
    
    }
    

      

    4:配置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" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    	http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    	http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    	<!-- 目标 实例化 -->
    	<bean id="Siming" class="com.proxy3.Siming"></bean>
    	<!-- 前置通知 -->
    	<bean id="BeforeAdvice" class="com.proxy3.BeforeAdvice"></bean>
    	<!-- 后置通知 -->
    	<bean id="AfterAdvice" class="com.proxy3.AfterAdvice"></bean>
    	<!-- 环绕通知 -->
    	<bean id="Methods" class="com.proxy3.Methods"></bean>
    
    	<!-- 某个方法回去触发通知 -->
    
    	<bean id="myBefore"
    		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    		<property name="advice" ref="BeforeAdvice"></property>
    		<property name="pattern" value=".*add.*"></property>
    	</bean>
    
    	<!-- 配置混合代理 -->
    	<bean id="myProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<!-- 引用目标 -->
    		<property name="target" ref="Siming"></property>
    		<!-- 目标实现的所有接口 -->
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.proxy3.IPerson3</value>
    			</list>
    		</property>
    		<!-- 配置通知 -->
    		<property name="interceptorNames">
    			<list>
    				<idref bean="Methods" />
    				<idref bean="myBefore" />
    
    				<idref bean="BeforeAdvice" />
    				<idref bean="AfterAdvice" />
    			</list>
    		</property>
    
    	</bean>
    </beans>
    

      这个配置文件里面有 前后通知 环绕通知的配置  感兴趣的可以看下 已经写的很详细了

    5:使用

    package com.proxy3;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class proxy3test {
    	@Test
    	public void seelp() {
    		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    		IPerson3 ip3 = (IPerson3) ac.getBean("myProxy");
    		// ip3.seelp();
    		ip3.cadd();
    	}
    }
    

      

    这些就是我自学的成果了 第一次写博客 如果写的不好敬请谅解 谢谢了

  • 相关阅读:
    [USACO][最短路]Cow Tours
    [USACO][枚举]Preface Numbering
    [USACO][枚举]Hamming Code
    [USACO][枚举]Healthy Holsteins
    [USACO][DAG上的动态规划]Sorting A Three-Valued Sequence
    [USACO][暴力]The Castle
    [USACO][枚举]Ski Course Design
    运算符重载must take either zero or one argument错误
    关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种
    鼠标滚轮事件封装
  • 原文地址:https://www.cnblogs.com/867143866qq/p/7726276.html
Copyright © 2020-2023  润新知