1.前置增强

接口:ISomeService

public interface ISomeService {
public void doSome();
}

public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log=========="); }
}
public class SomeService implements ISomeService{
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} }

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean>
<!--02.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property>
<property name="proxyTargetClass" value="true"></property>
</bean> </beans>

单测

//前置增强
@Test
public void test05(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");
SomeService service = (SomeService) ctx.getBean("proxyService");
service.doSome();
}

2.后置增强

接口:ISomeService

public interface ISomeService {
public void doSome();
}

public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("==========after=========="); }
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="afterReturning"></property>
</bean> </beans>

单测

//后置增强
@Test
public void test06(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}

3.环绕增强

接口:ISomeService

public interface ISomeService {
public void doSome();
}

public class MyMethodInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("before");
methodInvocation.proceed();
System.out.println("after");
return null;
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>

单测

   //环绕增强
@Test
public void test07(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}

4.异常增强

接口:ISomeService   Mystoo

public interface ISomeService {
public void doSome();
}
public interface Mystoo {
public void run();
public void run(String style);
}

public class MystooImpl implements Mystoo {
public void run() { }
public void run(String style) { }
}
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
int age=6/0;
System.out.println("错误");
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} public void doSecont() { }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean> <!--02.增强 通知-->
<bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean> <!--03.aop -->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property>
<!--做怎么样的增强-->
<property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>

单测

 //异常增强
@Test
public void test08(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
随笔 - 23  文章 - 0  评论 - 0

Spring增强

 

1.前置增强

接口:ISomeService

public interface ISomeService {
public void doSome();
}

public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log=========="); }
}
public class SomeService implements ISomeService{
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} }

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean>
<!--02.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property>
<property name="proxyTargetClass" value="true"></property>
</bean> </beans>

单测

//前置增强
@Test
public void test05(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");
SomeService service = (SomeService) ctx.getBean("proxyService");
service.doSome();
}

2.后置增强

接口:ISomeService

public interface ISomeService {
public void doSome();
}

public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("==========after=========="); }
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="afterReturning"></property>
</bean> </beans>

单测

//后置增强
@Test
public void test06(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}

3.环绕增强

接口:ISomeService

public interface ISomeService {
public void doSome();
}

public class MyMethodInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("before");
methodInvocation.proceed();
System.out.println("after");
return null;
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>

单测

   //环绕增强
@Test
public void test07(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}

4.异常增强

接口:ISomeService   Mystoo

public interface ISomeService {
public void doSome();
}
public interface Mystoo {
public void run();
public void run(String style);
}

public class MystooImpl implements Mystoo {
public void run() { }
public void run(String style) { }
}
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
int age=6/0;
System.out.println("错误");
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} public void doSecont() { }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean> <!--02.增强 通知-->
<bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean> <!--03.aop -->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property>
<!--做怎么样的增强-->
<property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>

单测

 //异常增强
@Test
public void test08(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
 
 
标签: Spring增强
好文要顶 关注我 收藏该文  
0
0
 
 
 
«上一篇:SPRING代理模式
»下一篇:Spring 顾问
posted @ 2017-07-31 17:03 <烟花易冷> 阅读(3) 评论(0) 编辑 收藏
 
 
发表评论

昵称:

评论内容:
     
 

退出 订阅评论

 

[Ctrl+Enter快捷键提交]

 
 
 

公告

昵称:<烟花易冷>
园龄:1年1个月
粉丝:4
关注:3

< 2017年8月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9
 
 
Copyright ©2017 <烟花易冷>

spring增强的更多相关文章

  1. Spring增强代理模式

    1. 依赖注入;(掌握) 2. XML自动注入;(掌握) 3. 全注解配置;(掌握) 4. 代理模式;(掌握,难点) 依赖注入 构造参数注入 constructor-arg:构造器注入: index: ...

  2. 基于XML配置的spring aop增强配置和使用

    在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...

  3. Spring横切面(advice),增强(advisor),切入点(PointCut)(转)

    Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解: 1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理 ...

  4. JavaEE Spring

    1.  Spring以一己之力撼动了Sun公司的JavaEE传统重量级框架(EJB),逐渐成为使用最多的JavaEE企业应用开发框架. 2.  Spring是分层的JavaEE应用一站式的轻量级开源框 ...

  5. Spring第二天

    Spring第二天 整体课程安排(3天+2天): 第一天:Spring框架入门.IoC控制反转的配置管理.Spring Web集成.Spring Junit集成. 第二天:Spring AOP面向切面 ...

  6. Spring AOP的实现及源码解析

    在介绍AOP之前,想必很多人都听说AOP是基于动态代理和反射来实现的,那么在看AOP之前,你需要弄懂什么是动态代理和反射及它们又是如何实现的. 想了解JDK的动态代理及反射的实现和源码分析,请参见下面 ...

  7. spring框架笔记

    Spring实现依赖注入的两种方式: 1.构造方法注入 2.set方法注入,p标签注入 Spring中事务的两种实现方式: 编程式事务管理 声明式事务管理(推荐) Spring增强类型: Before ...

  8. 什么。你还没有搞懂Spring事务增强器 ,一篇文章让你彻底搞懂Spring事务,虽然很长但是干货满满

    上一篇文章主要讲解了事务的Advisor是如何注册进Spring容器的,也讲解了Spring是如何将有配置事务的类配置上事务的,也讲解了Advisor,pointcut验证流程:但是还未提到的那个Ad ...

  9. 阿里四面:你知道Spring AOP创建Proxy的过程吗?

    Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...

随机推荐

  1. paramiko使用1

  2. linux strace-跟踪进程的系统调用或是信号产生情况,lstrace-跟踪己丑年调用库函数情况,进程跟踪调试命令

    本工具可以用来做大多数排除,比如mount一个NFS,很慢,找不出原因,我们可以使用strace命令来跟中mount这个经常所有的调用过程. strace 命令是一种强大的工具,它能够显示所有由用户空 ...

  3. 快速实现CentOS7安装python-pip

    1.首先检查linux有没有安装python-pip包,终端执行 pip -V [root@ network-scripts]# pip -V -bash: pip: command not foun ...

  4. 6、git和github

    参考:http://www.cnblogs.com/qianmojing/p/6484162.htmlhttp://www.jb51.net/article/70729.htmhttp://www.c ...

  5. 怎么查看Eclipse的版本信息

    工具/原料   Eclipse版本信息查看 第一种方法   1 找到Eclipse的解压目录就是你的Eclipse.exe 所在的目录 2 找到 .eclipseproduct 文件双击打开 3 如图 ...

  6. Docker删除私有仓库镜像

    V2 安装删除脚本 # curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/dele ...

  7. ZOJ - 3777(状压dp)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  8. LeetCode.11-装水最多的容器(Container With Most Water)

    这是悦乐书的第350次更新,第375篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第5题(顺位题号是11).给定n个非负整数a1,a2,-,an,其中每个表示坐标(i ...

  9. 遍历json字符串 并 写入对应的文本框

    1.js代码: function getFlws(){ var url = urlpath + "bhjk/getJson2.sd?"; $.post(url,function(d ...

  10. C#网络编程学习(6)---序列化和反序列化

    1.什么是序列化和反序列化 当客户端和服务器进行远程连接时,互相可以发送各种类型的数据.但都要先把这些对象转换为字节序列,才能在网络上进行传输. 序列化:就是发送方 把对象转换为字节序列的过程. 反序 ...