自学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();
}
}
这些就是我自学的成果了 第一次写博客 如果写的不好敬请谅解 谢谢了
自学spring AOP的更多相关文章
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
- spring aop
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- 基于Spring AOP的JDK动态代理和CGLIB代理
一.AOP的概念 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...
- Spring AOP详解
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- Spring AOP实例——异常处理和记录程序执行时间
实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...
- 从零开始学 Java - Spring AOP 实现用户权限验证
每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...
- 从零开始学 Java - Spring AOP 实现主从读写分离
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
随机推荐
- 201521123019 《Java程序设计》第2周学习总结
一. 本章学习总结 1.掌握了string类型的用法 2.对java数组有了初步了解 3.arrays用法有所掌握 二.书面作业 1.使用Eclipse关联jdk源代码,并查看String对象的源代码 ...
- java课程设计--WeTalk(201521123072秦贞一)
在线群聊系统 1,团队课程设计博客链接 http://www.cnblogs.com/slickghost/p/7018105.html 个人负责模块或任务说明 负责模块:界面设计与实现 2,自己的代 ...
- MD格式示例
一个例子: 例子开始 1. 本章学习总结 今天主要学习了三个知识点 封装 继承 多态 2. 书面作业 Q1. java HelloWorld命令中,HelloWorld这个参数是什么含义? 今天学了一 ...
- lintcode.245 子树
子树 描述 笔记 数据 评测 有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树. 注意事项 若 T1 中存在从节点 n 开始 ...
- Javascript跳转页面和打开新窗口等方法
1.在原来的窗体中直接跳转用onClick="window.location.href='你所要跳转的页面';" 2.在新窗体中打开页面用:onclick="window ...
- 记录兼容IE8中发现的一些问题
1.new Date().getYear(); chrome下:获取的是1900年之后的年份,如2017年获取的是117 IE8下:获取的是公元年份,如2017获取的是2017 解决方案:使用new ...
- Spring c3p0连接池无法释放解决方案
通过c3p0配置连接池的时候,在进行压力测试的时候,日志出现了这样一个错误:Data source rejected establishment of connection, message from ...
- nodejs中http-proxy使用小结
最近在写xmocker的工具,用于开发前期的mock数据,不可避免的用到了代理的中间件.当然,github上有关于http-proxy封装的中间件.毕竟是自己练手的项目,就自己写了个中间件,方便定制功 ...
- 分享ES6中比较常用又强大的新特性
前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)ABCD
A. Arpa and a research in Mexican wave time limit per test 1 second memory limit per test 256 megaby ...