Java基础-SSM之Spring的AOP编程

                                    作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

    Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法。它是对OOP的增强,适用于系统级功能。

 一.MethodBeforeAdvice接口的应用

1>.引入新的依赖

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.org.yinzhengjie</groupId>
<artifactId>MySpring</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency> <dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies> </project>

2>.定义通知类

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /**
* 前置通知
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
String mname = method.getName() ;
System.out.printf("当前处理的对象 : %s\n调用对象的方法 : %s\n调用参数 : %s\n" , target.toString(),mname.toString(),args.toString());
System.out.println("============= MyMethodBeforeAdvice代码执行完毕 =============");
}
}

3>.编写实现WelcomeService和WelcomeService2两个接口的类

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; public interface WelcomeService {
public abstract void sayHello();
}

WelcomeService.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; public interface WelcomeService2 {
public abstract void sayHello2();
}

WelcomeService2.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; public class WelcomeServiceImpl implements WelcomeService,WelcomeService2{
private String name ; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void sayHello() {
System.out.println(name);
} public void sayHello2() {
System.out.println("Hello Wold");
}
}

4>.编写Spring的配置文件

 <?xml version="1.0"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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="beforeAdvice" class="cn.org.yinzhengjie.spring.aop.advice.MyMethodBeforeAdvice" /> <!-- 目标对象 -->
<bean id="wsTarget" class="cn.org.yinzhengjie.spring.aop.advice.WelcomeServiceImpl">
<property name="name" value="I'm yinzhengjie !!!" />
</bean> <!-- 代理对象 -->
<bean id="wsProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>cn.org.yinzhengjie.spring.aop.advice.WelcomeService</value>
<value>cn.org.yinzhengjie.spring.aop.advice.WelcomeService2</value>
</list>
</property>
<!-- 拦截器名集合 -->
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
</list>
</property> <!-- 注意,目标类的名字为"target"关键字,而ref是真正需要被代理的对象连接。一个代理对象一次性只能代理一个类,如果想要代理多个类,需要重新单独定义bean标签哟! -->
<property name="target" ref="wsTarget" /> </bean>
</beans>

5>.编写单元测试类代码

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop {
@Test
public void testAOP1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml") ;
WelcomeService ws = (WelcomeService) ac.getBean("wsProxy");
ws.sayHello(); WelcomeService2 ws2 = (WelcomeService2)ws;
ws2.sayHello2(); }
}

  以上代码测试结果如下:

二.AOP的四个通知编程案例

  上面我们已经介绍了Spring的AOP编程中的一个前置通知,其实除了前置通知还有后置通知,循环通知以及异常通知。其实配置文件的方法和上面类似,只不过他们有各自的优点,下面我简单的做个介绍:

    前置通知:主要负责处理调用目标对象之前执行的代码;

    后置通知:主要负责处理调用目标对象之后的执行代码;

    环绕通知:它执行的优先级是在后置通知之前,在前置通知之后的执行的代码,它可以把方法包起来处理,比如在调用方法之前执行一段代码,在调用方法之后,还可以执行一段代码,另外,相比前置通知和通知通知,它还新增了一个返回值的功能;

    异常通知:它执行的优先级暂时没法确定,而且不一定执行,顾名思义,它是在有异常的时候才会触发的代码!因此即使你写了这样的通知,有可能你看不到它的执行结果,除非你故意写一段可能出现异常的代码;

1>.编写测试代码

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; public interface WelcomeService {
public abstract void sayHello();
}

WelcomeService.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; public interface WelcomeService2 {
public abstract void sayHello2();
}

WelcomeService2.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; public class WelcomeServiceImpl implements WelcomeService,WelcomeService2{
private String name ; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void sayHello() {
System.out.println(name);
} public void sayHello2() {
System.out.println("Hello Wold");
}
}

WelcomeServiceImpl.java 文件内容

2>.编写通知类

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /**
* 前置通知
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
String mname = method.getName() ;
System.out.printf("当前处理的对象 : %s\n调用对象的方法 : %s\n调用参数 : %s\n" , target.toString(),mname.toString(),args.toString());
System.out.println("============= MyMethodBeforeAdvice代码执行完毕 =============");
}
}

MyMethodBeforeAdvice.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; /**
* 环绕通知,可以篡改行为
*/
public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("begin");
//调用目标对象方法的返回值
Object tis = invocation.getThis();
System.out.println(tis);
Object ret = invocation.proceed() ;
System.out.println("end");
return ret;
}
}

MyMethodInterceptor.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("over");
}
}

MyAfterReturningAdvice.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; /**
* 异常通知
*/
public class MyThrowableAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
System.out.println("出事了!!" + ex.toString());
}
}

MyThrowableAdvice.java 文件内容

3>.编写Spring的配置文件

 <?xml version="1.0"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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="beforeAdvice" class="cn.org.yinzhengjie.spring.aop.advice.MyMethodBeforeAdvice" />
<bean id="afterAdvice" class="cn.org.yinzhengjie.spring.aop.advice.MyAfterReturningAdvice" />
<bean id="aroundAdvice" class="cn.org.yinzhengjie.spring.aop.advice.MyMethodInterceptor" />
<bean id="throwableAdvice" class="cn.org.yinzhengjie.spring.aop.advice.MyThrowableAdvice" /> <!-- 目标对象 -->
<bean id="wsTarget" class="cn.org.yinzhengjie.spring.aop.advice.WelcomeServiceImpl">
<property name="name" value="tom" />
</bean> <!-- 代理对象 -->
<bean id="wsProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>cn.org.yinzhengjie.spring.aop.advice.WelcomeService</value>
<value>cn.org.yinzhengjie.spring.aop.advice.WelcomeService2</value>
</list>
</property>
<!-- 拦截器名集合 -->
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>aroundAdvice</value>
<value>throwableAdvice</value>
</list>
</property>
<property name="target" ref="wsTarget" />
</bean>
</beans>

4>.编写测试代码

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.aop.advice; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop {
@Test
public void testAOP1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml") ;
WelcomeService ws = (WelcomeService) ac.getBean("wsProxy");
ws.sayHello(); WelcomeService2 ws2 = (WelcomeService2)ws;
ws2.sayHello2(); }
}

5>.运行以上代码执行结果如下:

Java基础-SSM之Spring的AOP编程的更多相关文章

  1. Java基础-SSM之Spring的POJO(Plain Old Java Object)实现AOP

    Java基础-SSM之Spring的POJO(Plain Old Java Object)实现AOP 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 上次我分享过Spring传统的A ...

  2. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  3. Java基础-SSM之Spring和Mybatis整合案例

    Java基础-SSM之Spring和Mybatis整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   在之前我分享过mybatis和Spring的配置案例,想必大家对它们的 ...

  4. Java基础-SSM之Spring MVC入门篇

    Java基础-SSM之Spring MVC入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spring MVC简介 1>.什么是Spring MVC 答:Sprin ...

  5. Java基础-SSM之Spring快速入门篇

    Java基础-SSM之Spring快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java ...

  6. Java进阶知识21 Spring的AOP编程

    1.概述 Aop:(Aspect Oriented Programming)面向切面编程          功能: 让关注点代码与业务代码分离! 关注点:重复代码就叫做关注点:切面: 关注点形成的类, ...

  7. Java基础-SSM之mybatis快速入门篇

    Java基础-SSM之mybatis快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 其实你可能会问什么是SSM,简单的说就是spring mvc + Spring + m ...

  8. Java基础复习笔记系列 九 网络编程

    Java基础复习笔记系列之 网络编程 学习资料参考: 1.http://www.icoolxue.com/ 2. 1.网络编程的基础概念. TCP/IP协议:Socket编程:IP地址. 中国和美国之 ...

  9. Java基础复习笔记系列 八 多线程编程

    Java基础复习笔记系列之 多线程编程 参考地址: http://blog.csdn.net/xuweilinjijis/article/details/8878649 今天的故事,让我们从上面这个图 ...

随机推荐

  1. python删除文件与目录的方法

    python内置方法删除目录(空目录与非空目录)及文件 1.os.remove(file_path):删除文件 #PPTV是文件夹,xen.txt是文件 >>> os.remove( ...

  2. [C#源代码]使用SCPI指令对通信端口(RS232/USB/GPIB/LAN)进行仪器编程

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 本软件是基于NI-VISA/VISA32(Virtual Instrument Softwar ...

  3. JavaScript快速入门-实战(例子)

    1.模拟bootstrap中的模态框 效果图:(点我后,弹出对话框,最下面的内容可以看到,但是有一定的透明度.) 思路分析: 整体分为三层,最底层(点我),中间透明层(实现透明效果),最顶层(最新内容 ...

  4. 【原创】CA证书申请+IIS配置HTTPS+默认访问https路径

    一.CA证书申请 (一). 新StartSSL注册帐号 1.    StartSSL官网 官方网站:https://www.startssl.com/ 2.    进入到StartSSL后,直接点击注 ...

  5. 复习下VLAN的知识

    转载:来自百度百科 VLAN一般指虚拟局域网 VLAN(Virtual Local Area Network)的中文名为"虚拟局域网". 虚拟局域网(VLAN)是一组逻辑上的设备和 ...

  6. 区块链--Bitcoin共识机制

    目录 中心化和去中心化 比特币共识机制 拜占庭将军共识机制 比特币成功解决了拜占庭问题 中心化和去中心化 中心化模式: 优点:效率高 缺点:中间层次太多(组织层次连接) 去中心化模式: 缺点:效率低 ...

  7. mysql新监语句需要前面加SET FOREIGN_KEY_CHECKS=0;

    SET FOREIGN_KEY_CHECKS=0; -- ------------------------------ Table structure for guestbook-- -------- ...

  8. Linux内核分析第五章读书笔记

    第五章 系统调用 在操作系统中,内核提供了用户进程与内核进行交互的一组接口,这些接口在应用程序和内核之间扮演了使者的角色,保证系统稳定可靠,避免应用程序肆意妄行. 5.1 与内核通信 系统调用在用户空 ...

  9. 框架-Spring

    项目中都用到了Spring.Mybatis.SpringMVC框架,首先来谈一谈Spring框架,Spring框架以IOC.AOP作为主要思想. IOC----控制反转 IOC的全称为Inversio ...

  10. Git(2.14.1版本)学习及使用(一)

    OuZeBo原创作品.转载请注明出处 http://www.cnblogs.com/OuZeBo/p/7477465.html 1.下载git:https://git-scm.com/ 2.安装(本人 ...