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. Nginx日常报错处理总结

    在Nginx错误日志中,有大量的下列信息: Upstream timed out (110: Connection timed out) while reading response header f ...

  2. vue-router单页应用简单示例(一)

    请先完成了项目初始化,具体请看我另一篇博文.vue项目初始化 看一下完成的效果图,很典型的单页应用. .vue后缀名的单文件组件   这里先说一下我对组件的理解.组件,顾名思义就是一组元素组成的一个原 ...

  3. 浅谈我的UI设计之路

    时光匆匆,进入UI学习已经快两个月了,这段时间过得很充实,因为有压力才有收获. 还记的刚刚学习手绘的时候,对于这个行业只有一个初步的认识,知道自己喜欢,但是真正学习的时候才发现,我要学习的东西还有很多 ...

  4. PHP Lavavel 使用控制器 传递变量 以及调用 视图模板

    控制器第一次入门使用 位置: 在app/Http/Controllers 目录下创建文件名格式:例如 UserController路由调用格式:Route::get('user/tom','UserC ...

  5. webWorker

    一.webWorker之初体验 在"setTimeout那些事儿"中,说到JavaScript是单线程.也就是同一时间只能做同一事情. 也好理解,作为浏览器脚本语言,如果JavaS ...

  6. 《Linux内核设计与实现》第18章读书整理

    第十八章.调试 18.1 准备开始 如果bug能重现的话,将会有很大的帮助. 18.2 内核中的bug Bug多种多样,产生的原因可以有无数的原因,表象也变化多端. 从隐藏在源代码中的错误到展现在目击 ...

  7. 使用SSH过程中遇到的几个问题及解决方案

    一.HTTP Status 500 - org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: B ...

  8. Cooperate with Myself

    (一) 第一周的第一批作业们.  且不说一周之内要看完我们的300多页的教材,也不说需要在维基的大批量的文献中海底捞针,单是这个四则运算的生成程序就让我从假期的迷糊状态中幡然觉悟了:哦!惊险刺激的新的 ...

  9. Mybatis:Eclipse引入dtd约束文件使得xml文件有提示

    https://blog.csdn.net/lsx2017/article/details/82558135

  10. canvas实现五子棋界面

    1.获取canvas画布 var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); ...