1.实现前置增强 必须实现接口MethodBeforeAdvice接口

创建对应的文件

public interface Animal {//主业务接口
void eat(); //目标方法
void sleep();
}

Animal接口

public class Dog implements Animal {//目标对象
/*
* 专心我们的逻辑
* 至于方法之前和方法之后要做的事情!我不关注!
* Ioc:降低了主业务之间的耦合度
* AOP:降低了主业务和交叉业务逻辑之间的耦合度
*/
@Override
public void eat() {
System.out.println("Dog在吃骨头");
} @Override
public void sleep() {
System.out.println("Dog睡觉");
} }

Dog实现类

public class AnimalBeforeAdvice implements MethodBeforeAdvice {
/**
* method:执行的方法
* args:方法的参数
* target:目标类对象
*/
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("进入 了 前置 增强..............MethodBeforeAdvice");
} }

前置增强类

<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/> <!-- 配置前置 增强 -->
<bean id="before" class="cn.bdqn.advice.AnimalBeforeAdvice"/> <!-- 生成 代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象(被代理的对象)<property name="targetName" value="dog"/> -->
<property name="target" ref="dog"/>
<!-- 设置增强方式 -->
<property name="interceptorNames">
<value>before</value>
</property>
</bean> </beans>

applicationContext.xml文件

public class AnimalTest {

    @Test
public void test01(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
//使用代理对象进行操作
Animal animal=(Animal) context.getBean("proxyFactoryBean");
animal.eat();
System.out.println("************");
animal.sleep();
}
}

测试类

2.实现后置增强 必须实现接口AfterReturningAdvice接口

public class AnimalAfterAdvice implements AfterReturningAdvice {
/**
* returnValue:返回值 ,能获取返回值 但是如果对返回值进行了操作!没有意义!
* afterReturning()就没有返回值
*/
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("执行了 后置 通知.................AfterReturningAdvice");
} }

后置增强类

<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/> <!-- 配置前置 通知-->
<bean id="before" class="cn.bdqn.advice.AnimalBeforeAdvice"/>
<!-- 配置后置 通知-->
<bean id="after" class="cn.bdqn.advice.AnimalAfterAdvice"/> <!-- 生成 代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象(被代理的对象)<property name="targetName" value="dog"/> -->
<property name="target" ref="dog"/>
<!-- 设置增强方式 同时配置多个增强方式01 .value="before,after"-->
<property name="interceptorNames" >
<!-- 02. 使用array
<array>
<value>before</value>
<value>after</value>
</array> -->
<list>
<value>before</value>
<value>after</value>
</list>
</property>
</bean> </beans>

修改后的xml文件

测试代码不变 直接运行

3.实现环绕增强 必须实现接口MethodInterceptor接口

public class AnimalAroundAdvice implements MethodInterceptor {
/**
*
* 环绕增强 是在前置增强之后 和后置增强之前 执行!
* 可以对方法的返回值进行一系列的操作
*/
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("方法之前........MethodInterceptor");
Object result = invocation.proceed();
//判断是否有返回值
if (result!=null) {
result=((String)result).toUpperCase();
} System.out.println("方法之后........MethodInterceptor");
return result;
} }

环绕增强类

可以修改Animal中的任意一个方法带有返回值的进行测试! 如

public interface Animal {//主业务接口
String eat(); //目标方法
void sleep();
}

改变后的Animal接口

public class Dog implements Animal {//目标对象
@Override
public String eat() {
System.out.println("Dog在吃骨头");
return "abcd";
} @Override
public void sleep() {
System.out.println("Dog睡觉");
} }

改变后的Dog实现类

<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/>
<!-- 配置环绕 通知-->
<bean id="around" class="cn.bdqn.advice.AnimalAroundAdvice"/> <!-- 生成 代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="dog"/>
<property name="interceptorNames" >
<list>
<value>around</value>
</list>
</property>
</bean> </beans>

xml文件

    //测试返回值
@Test
public void test02(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
//使用代理对象进行操作
Animal animal=(Animal) context.getBean("proxyFactoryBean");
System.out.println(animal.eat());
System.out.println("************");
animal.sleep();
}

测试类

效果图

4.实现自定义增强

并不是所有的方法都需要增强,那么我们就需要自定义增强的方法

<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/> <!-- 配置前置 通知-->
<bean id="before" class="cn.bdqn.advice.AnimalBeforeAdvice"/> <!-- 自定义通知的切入点 -->
<bean id="myAdvice" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="before"></property>
<property name="mappedNames">
<list>
<value>eat</value> <!-- eat之外的方法就不会有前置通知了 -->
</list>
</property>
</bean> <!-- 生成 代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="dog"/>
<property name="interceptorNames" >
<list>
<value>myAdvice</value> <!--引入自定义增强 -->
</list>
</property>
</bean> </beans>

修改后的xml文件

测试代码不变

spring06Aop的更多相关文章

随机推荐

  1. C++封装常用对象和对头文件探索

    在C++实际开发中,难免会使用到一些你极为常用的算法(比如笔者经常使用的多线程技术),实现这些算法的类或是全局函数或是命名空间等等经常都要被使用多次,你会有哪些办法来使用呢?笔者有4个办法. 第一个方 ...

  2. 10 款强大的JavaScript图表图形插件推荐

    转自:http://www.iteye.com/news/24535 网上有很多用于绘制图表图形的免费JavaScript插件和图表库,这类插件大量出现的原因,一是人们不再依赖于Flash,二是浏览器 ...

  3. z-index的理解 z-index 属性仅在节点的 position 属性为 relative, absolute 或者 fixed 时生效.

    今天做游戏的Exercise模式的时候,发现把所有的div设置为position:absolute;后,点击play进入到游戏界面的时候,鼠标点击数字的时候,完全没反应.经过我的反复检查,发现只要给所 ...

  4. $_GLOBALS超全局数组和global定义的全局变量区别?

    全局变量:主程序中定义的变量(函数外部),只能在主程序中使用,在函数内部不能调用 背景:解决在函数内部调用全局变量的问题 解决方法: 1.在函数内部声名全局变量 <?php public $va ...

  5. 微信公众平台Js API(WeixinApi)

    微信公众平台Js API(WeixinApi): https://github.com/zxlie/WeixinApi#user-content-3%E9%9A%90%E8%97%8F%E5%BA%9 ...

  6. CentOS 5.6服务器配置YUM安装Apache+php+Mysql+phpmyadmin

    1. 更新系统内核到最新. [root@linuxfei ~]#yum -y update 系统更新后,如果yum安装时提示错误信息,请执行以下命令修复. [root@linuxfei ~]#rpm ...

  7. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

  8. SQL中 WHERE与HAVING的区别

    SQL语句中的Having子句与where子句之区别 在说区别之前,得先介绍GROUP BY这个子句,而在说GROUP子句前,又得先说说“聚合函数”——SQL语言中一种特殊的函数.例如SUM, COU ...

  9. poj 1606Jugs

    http://poj.org/problem?id=1606 #include<cstdio> #include<cstring> #define MAXN 1000000 u ...

  10. sql server 2005中使用with实现递归

    WITH fw_requestion_note_temp(old_apply_id) AS ( --取根节点放入临时表 SELECT old_apply_id FROM fw_requestion_n ...