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

本文介绍了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();
}
}

  

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

自学spring AOP的更多相关文章

  1. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  2. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  3. spring aop

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...

  4. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  5. 基于Spring AOP的JDK动态代理和CGLIB代理

    一.AOP的概念  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...

  6. Spring AOP详解

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  7. Spring AOP实例——异常处理和记录程序执行时间

    实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...

  8. 从零开始学 Java - Spring AOP 实现用户权限验证

    每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...

  9. 从零开始学 Java - Spring AOP 实现主从读写分离

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

随机推荐

  1. 201521123106 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  2. Markdown格式范例

    一个例子: 例子开始 1. 本章学习总结 今天主要学习了三个知识点 封装 继承 多态 2. 书面作业 Q1. java HelloWorld命令中,HelloWorld这个参数是什么含义? 今天学了一 ...

  3. MapReduce执行过程

    Mapper任务的执行过程: 第一阶段是把输入文件按照一定的标准分片(InputSplit),每个输入片的大小是固定的.默认情况下,输入片(InputSplit)的大小与数据块(Block)的大小是相 ...

  4. [js学习笔记] 原型链理解

    js中只有对象,包括对象,函数,常量等. prototype 只有函数里有这个属性. 对象里可以设置这个属性,但是没这个属性的意义 prototype指向一个对象,可以添加想要的方法. 该对象里有一个 ...

  5. win7系统Myeclipse下切换SVN用户

     Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密码保存之后 ...

  6. vue 父组件向子组件传递事件/调用事件

    方法一:子组件监听父组件发送的方法 方法二:父组件调用子组件方法 子组件: export default { mounted: function () { this.$nextTick(functio ...

  7. AngularJS–Animations(动画)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/   在AngularJS 1.3 中,给一些指令(eg:   ngRepeat,ngSw ...

  8. Delphi中 StrToIntDef函数的用法

    Delphi中 StrToIntDef函数的用法:比如我要判断一个文本框里输入的字符串能不能转换为integer类型,如果能,则返回转换后的整型数据,如果不能,则返回整数0,那么我就可以用strtoi ...

  9. 【转】Keberos认证原理

    前几天在给人解释Windows是如何通过Kerberos进行Authentication的时候,讲了半天也别把那位老兄讲明白,还差点把自己给绕进去.后来想想原因有以下两点:对于一个没有完全不了解Ker ...

  10. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...