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. C#设计模式(7)——适配器模式

    一.概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 二.模型 三.代码实现 using System; /// 这里以 ...

  2. 使用struts2进行文件下载以及下载权限控制的例子

    本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流. 下面直接上我的代码: 登录表 ...

  3. Windchill 配置LOG文件,使开发中的代码能显示打印的信息

    如开发代码的类HomeLogic.java, 包路径在pnt.report.home 需求:需监控此类的打印数据 方法:配置D:\ptc\Windchill_10.1\Windchill\codeba ...

  4. 使用PM2管理nodejs进程分享

    摘要:pm2 是一个带有负载均衡功能的Node应用的进程管理器.本文主要介绍了详解使用PM2管理nodejs进程,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧,希望能帮助 ...

  5. assert.strictEqual()

    assert.strictEqual(actual, expected[, message]) 使用全等运算符(===)测试 actual 参数与 expected 参数是否全等. // 格式 ass ...

  6. 树莓派 Learning 002 装机后的必要操作 --- 01 解决上网问题

    树莓派 装机后的必要操作 - 解决上网问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 装机后的必要操作 解决上网问题 解决上网 ...

  7. xgene:肿瘤相关基因 KRAS,,BRAF,,通路PI3K-AKT

    KRAS基因 一个是KRAS1,位于chr6 短臂上,是一个“假基因”,它不能被转录成RNA,故没有功能的 另一个是KRAS2,位于chr12 短臂上..是“真基因”,是能够转录.并且翻译成蛋白的,是 ...

  8. SpringMVC的国际化

    关于SpringMVC的国际化,http://www.cnblogs.com/liukemng/p/3750117.html这篇文章已经讲的很好了.它讲了有如下几种国际化方式 1:基于Http的hea ...

  9. return die exit 常用

    die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值die是遇到错误才停止exit是直接停止,并且不运行后续代码,exit()可以显示内容.return就是纯粹的返回 ...

  10. 中山纪念中学20170310洗衣服(贪心,优先队列升序【pair】)

    #include<bits/stdc++.h>using namespace std;typedef pair<long long,int>clot;priority_queu ...