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

本文介绍了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. 201521123117 《Java程序设计》第1周学习总结

    第一周学习总结: 这周开始学习了Java,因为之前C语言没学好所以看部分东西还是一头雾水,但是在查阅资料和同学的帮助开始对Java有了一些了解,这周主要学习了Java的诞生发展以及运用包括JVN/JR ...

  2. Java-高效地使用Exception-实践

    注:本文翻译自Exceptional practices,共分为3节.该文章从实践的角度非常透彻地解释了为什么要处理异常,以及如何处理异常.这些都是那些只会介绍trycatchfinally语法的教材 ...

  3. 201521123010 《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  4. Android 跳转系统选择本地视频的功能

    今天在项目开发的过程中产品要求添加选择本地视频的功能,于是就翻阅和查找各种资料,进行功能的开发,但是在开发过程中发现,各种不同的品牌的手机跳转至系统选择本地视频的功能结果不太一样,所以我就对一些主流的 ...

  5. 生成/etc/shadow文件中的密码

    shadow文件的格式就不说了.就说说它的第二列--密码列. 通常,passwd直接为用户指定密码就ok了.但在某些情况下,要为待创建的用户事先指定密码,还要求是加密后的密码,例如kickstart文 ...

  6. NativeScript官方书籍:NativeScript-用你现有技术构建移动应用程序

    大家好,我用nativescript做企业级移动应用开发一年多了.从最初只能看nativescript英文文档,到现在看到官方发布正式的书籍,感觉nativescript变得越来越好. 当然,在这个过 ...

  7. Bootstrap Table急速完美搭建后台管理系统

    Bootstrap Table是基于 Bootstrap 的 jQuery 表格插件,通过简单的设置,就可以拥有强大的单选.多选.排序.分页,以及编辑.导出.过滤(扩展)等等的功能:http://bo ...

  8. mongodb 与关系型数据库

    设计 MongDB 模式时应注意的问题 根据用户需求来设计模式. 如果想一起使用对象,请将这些对象合并到一个文档中,否则要将它们分开(但是要确保不需要连接). 经常复制数据(但要有一定限度),因为与计 ...

  9. WebSocket In ASP.NET Core(二)

    WebSocket In ASP.NET Core(二) Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error ...

  10. 【SQL】- 基础知识梳理(四) - 存储过程

    存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行 存储过程的好处 A. 存储过程允许标准组件式编程  ...