spring的aop
来源:互联网 发布:java createelement 编辑:程序博客网 时间:2024/06/06 01:37
转自http://pandonix.iteye.com/blog/336873/
package com.lam.spring;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;/** * need aspectjrt.jar, aspectjweaver.jar *@Title:ServiceAspect.java *@Description:TODO *@Author:Administrator *@Date:2015年4月14日 下午10:30:26 *@Version:1.0 */public class ServiceAspect {public void doAfter(JoinPoint jp){System.out.println("after method:" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());}public Object doAround(ProceedingJoinPoint pjp) throws Throwable{long startTime = System.currentTimeMillis();Object ret = pjp.proceed();long endTime = System.currentTimeMillis();System.out.println("process time:" + (endTime - startTime));return ret;}public void doBefore(JoinPoint jp){System.out.println("before method:" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());}public void doThrowing(JoinPoint jp, Throwable th){System.out.println("method:" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw Throwable. " + th.getMessage());}}
package com.lam.spring;/** *@Title:AService.java *@Description:TODO *@Author:Administrator *@Date:2015年4月14日 下午10:22:37 *@Version:1.0 */public interface AService {public void fooA(String msg);public void barA();}
/** *@Title:AServiceImpl.java *@Description:TODO *@Author:Administrator *@Date:2015年4月14日 下午10:23:34 *@Version:1.0 */public class AServiceImpl implements AService{@Overridepublic void fooA(String msg) {System.out.println("AServiceImpl.fooA, msg" + msg);}@Overridepublic void barA() {System.out.println("AServiceImpl.barA");}}
package com.lam.spring;/** *@Title:BServiceImpl.java *@Description:TODO *@Author:Administrator *@Date:2015年4月14日 下午10:24:39 *@Version:1.0 */public class BServiceImpl {public void barB(String msg, int type){System.out.println("BServiceImpl.barB, msg:" + msg + ", type:" + type);if(type == -1){throw new IllegalArgumentException("type:" + type);}}public void fooB(){System.out.println("AServiceImpl.fooB");}}
public class AopTest {public static void main(String[] args) {ApplicationContext appCtx = new ClassPathXmlApplicationContext("file:F:\\...\\applicationContext.xml"); AService aService = (AService) appCtx.getBean("aService"); BServiceImpl bService = (BServiceImpl) appCtx.getBean("bService"); aService.barA(); aService.fooA("I am arguments A."); bService.barB("I am arguments B.", -1); bService.fooB(); }}
spring的配置xml文件:
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" > <aop:config> <aop:aspect id="ServiceAspect" ref="serviceAspect"> <!--配置com.lam.spring包下所有类或接口的所有方法--> <aop:pointcut id="businessService" expression="execution(* com.lam.spring.*.*(..))" /> <aop:before pointcut-ref="businessService" method="doBefore"/> <aop:after pointcut-ref="businessService" method="doAfter"/> <aop:around pointcut-ref="businessService" method="doAround"/> <!-- th是切面处理类ServiceAspect的doThrowing方法的第二个参数,名字要一样 --> <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="th"/> </aop:aspect> </aop:config> <bean id="serviceAspect" class="com.lam.spring.ServiceAspect" /> <bean id="aService" class="com.lam.spring.AServiceImpl"></bean> <bean id="bService" class="com.lam.spring.BServiceImpl"></bean> </beans>...
0 0
- AOP、Spring的AOP
- spring(AOP)Aop的概念
- spring的的AOP
- Spring的AOP实现
- spring的AOP介绍
- Spring的aop
- 6.2 Spring的AOP
- Spring 的AOP运用
- spring aop的理解
- spring aop的讲解
- spring的aop原理
- spring的aop
- spring的AOP配置
- Spring AOP的应用
- Spring的AOP
- Spring的AOP配置
- Spring的aop
- Spring Aop的应用
- Android ImageView设置长度高度为wrap_content时高度根据图片比例自适应
- 火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件
- java定时任务
- Maven+SpringMVC+Hibernate项目框架搭建
- leetcode 日经贴,Cpp code -Largest Number
- spring的aop
- jQuery选择器与js获得的一点不同
- [LeetCode 124]Binary Tree Maximum Path Sum
- nmap使用手册
- 我理解的剑指Offer--------链表中倒数第k个结点
- GPG入门教程
- 谷歌搜索表单参数url参数详解
- 我的C语言复习笔记
- Xcode 6本地化iOS程序