设为首页 加入收藏

TOP

Spring AOP 中 advice 的四种类型 before after throwing advice around(二)
2014-11-24 11:59:58 来源: 作者: 【 】 浏览:116
Tags:Spring AOP advice 类型 before after throwing around
fining beans [book]; root of factory hierarchy
---------------------
Book name Effective java
---------------------
Book URL www.google.cn
----------------------
下面对以上的Book加上Spring AOP advices
1:before advice
before advice将在方法执行前执行,创建一个实现MethodBeforeAdvice接口的类能够定义执行方法前的操作。
类如下:
[java]
package com.myapp.core.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeMethod implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("Before Method");
System.out.println("--------------------");
}
}
配置对应的bean:
在aop.xml中配置,创建一个BeforeMethod类,一个新的代理命名为:bookProxy
1: target 设置你想拦截的bean
2:interceptorNames设置通知,你想作用于proxy/target上的
对应的配置文件如下:
[html]
< xml version="1.0" encoding="UTF-8" >
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-3.0.xsd">
beforeMethodBean
注意:为了使用proxy(代理)我们需要引入 CGLIB2, pom.xml文件中注入如下:
[html]
cglib
cglib
2.2.2
运行测试类:
[java]
package com.myapp.core.aop.advice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("resource/aop.xml");
Book book = (Book) context.getBean("bookProxy");
System.out.println("---------------------");
book.printName();
System.out.println("---------------------");
book.printUrl();
System.out.println("----------------------");
try{
book.printThrowException();
}catch(Exception e){
// e.printStackTrace();
}
}
}
注意以上获得的是代理bean;
运行结果如下:
[plain]
三月 20, 2013 2:18:56 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 14:18:55 CST 2013]; root of context hierarchy
三月 20, 2013 2:18:56 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resource/aop.xml]
三月 20, 2013 2:18:57 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157985: defining beans [book,beforeMethodBean,bookPro
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇图的最短路径dijkstra算法 下一篇Java5 多线程--Semaphore实现信号..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)