Spring AOP 配置
来源:互联网 发布:淘宝悬浮导航制作方法 编辑:程序博客网 时间:2024/06/07 12:57
基础篇
1、理解AOP
在不同的类方法中,为了执行相同的逻辑,设定了许多相同的插入点。这些点构成了一个切面,一刀切入到这些类方法中。
2、理解Aspect
Aspect是对系统中横切面关注点逻辑进行模块化封装的AOP概念实体。
包含多个Pointcut和Advice。
3、理解名词
1) Joinpoint
插入到方法中的点。
2)Pointcut
Joinpoint 构成的切面。
3)Advice
需要在Joinpoint出执行的逻辑。
配置篇
一、所需要的jar包
这是使用spring3.0.4时,所用到的包。
aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
commons-logging-1.1.jar
org.springframework.aop-3.0.4.RELEASE.jar
org.springframework.asm-3.0.4.RELEASE.jar
org.springframework.aspects-3.0.4.RELEASE.jar
org.springframework.beans-3.0.4.RELEASE.jar
org.springframework.context-3.0.4.RELEASE.jar
org.springframework.core-3.0.4.RELEASE.jar
org.springframework.expression-3.0.4.RELEASE.jar
注:其中context包依赖beans包。
二、配置出现了异常
1) 这是指该构造函数缺少了BeanException异常包。
The constructor ClassPathXmlApplicationContext(String) refers to the missing type BeanException;
所以我们添加了org.springframework.beans-3.0.4.RELEASE.jar
2)不能代理目标对象。这和java的动态代理机制有关。
Exception:
Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces
解决方法:
1、将目标对象也就是要代理的对象bean,去实现一个接口IBean;
2、添加cglib2包。
代码如下:
//这是beanpackage com.beans; public interface IBean { public void fry(); public void cook(); public void stew(); }package com.beans;public class BroadBean implements IBean { public void fry(){ System.out.println("这是炒的方法"); } public void stew() { System.out.println("这是煮的方法"); } public void cook() { System.out.println("这是烹的方法"); }} //这是切面@Aspectpublic class BroadBeanInterceptor { @Pointcut("execution(* com.beans.BroadBean.*(..))") public void eatBean() { System.out.println("I am eating...");//Joinpoint 不会执行 } @Before("eatBean()") public void hitBean() { System.out.println("I am hitting..."); } @Around("eatBean()") public Object perofrmanceTrace(ProceedingJoinPoint joinpoint) throws Throwable { StopWatch watch = new StopWatch(); try{ watch.start(); return joinpoint.proceed(); } finally { watch.stop(); } } @After("eatBean()") public void pick() { System.out.println("I am picking..."); }}//这是测试public class AOPTest { @Test public void testAOP() { ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IBean b = (IBean) appContext.getBean("broadBean"); b.cook(); b.fry(); }}
输出为:
I am hitting...
这是烹的方法
I am picking...
I am hitting...
这是炒的方法
I am picking...
--完--
- Spring AOP事务配置
- spring aop配置2
- Spring AOP配置选项
- spring的AOP配置
- spring aop 事务配置
- spring aop 配置
- Spring的AOP配置
- spring AOP零配置
- Spring MVC 配置 AOP
- Spring的AOP配置
- Spring的AOP配置
- spring aop 配置
- Spring AOP配置
- spring配置文件配置aop
- spring aop配置
- spring Aop 配置
- spring aop 配置
- Spring的AOP配置
- C# POST数据到指定url并返回结果页面内容
- spring(4)_ bean属性 scope:作用域和lazy-init
- Ubuntu12.04 下配置qtcreator交叉编译
- [Audio] Android 耳机切换
- 不使用JQuery的多行新闻滚动
- Spring AOP 配置
- 窗口句柄浅析和获取窗口句柄的方法
- [Python]GUI编程练习 -- 获取天气预报
- DOM节点的操作
- 反射原理获得bean的属性
- OpenWRT 增加内核模块及应用方法
- libpcap使用
- C#启动另一个应用程序并传参数
- .Net解析JSON数据