aop (!void,用来拦截有返回值的函数)
来源:互联网 发布:iphone保护壳 知乎 编辑:程序博客网 时间:2024/05/16 12:18
cn.itcast.service
- package cn.itcast.service;
- public interface PersonService {
- public void save(String name);
- public void update(String name, Integer id);
- public String getPersonName(Integer id);
- }
- package cn.itcast.service;
- import org.aspectj.lang.ProceedingJoinPoint;
- /**
- * 切面
- *
- */
- public class MyInterceptor {
- public void doAccessCheck() {
- System.out.println("前置通知");
- }
- public void doAfterReturning() {
- System.out.println("后置通知");
- }
- public void doAfter() {
- System.out.println("最终通知");
- }
- public void doAfterThrowing() {
- System.out.println("例外通知");
- }
- public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("进入方法");
- Object result = pjp.proceed();
- System.out.println("退出方法");
- return result;
- }
- }
cn.itcast.service.impl
- package cn.itcast.service.impl;
- import cn.itcast.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public String getPersonName(Integer id) {
- System.out.println("我是getPersonName()方法");
- return "xxx";
- }
- public void save(String name) {
- //throw new RuntimeException("我爱例外");
- System.out.println("我是save()方法");
- }
- public void update(String name, Integer id) {
- System.out.println("我是update()方法");
- }
- }
junit.test
- package junit.test;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.service.PersonService;
- public class testAOP {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test public void interceptorTest(){
- ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
- PersonService personService = (PersonService)cxt.getBean("personService");
- personService.getPersonName(2);
- }
- }
beans.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/> <aop:config> <aop:aspect id="asp" ref="aspetbean"> <!-- 第一个代表返回值类型,中间的代表包,括号中的代表参数, --> <!-- 这样写可以拦截有返回值的函数 --> <aop:pointcut id="mycut" expression="execution(!void cn.itcast.service..*.*(..))"/> <aop:before pointcut-ref="mycut" method="doAccessCheck"/> <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/> <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/> <aop:after pointcut-ref="mycut" method="doAfter"/> <aop:around pointcut-ref="mycut" method="doBasicProfiling"/> </aop:aspect> </aop:config></beans>
- aop (!void,用来拦截有返回值的函数)
- main 函数返回值void的警告
- 函数参数返回值 void 和 void *
- C中-void类型函数同样可以让函数有返回值
- 有返回值的函数
- 有返回值的函数
- 戒除main函数返回值为void的坏习惯
- main函数的返回值为void对吗?
- void * 和 void 在函数返回值中的区别
- 为什么C++中stack的pop()函数不返回值而返回void
- 为什么C++中stack/queue的pop()函数不返回值而返回void
- InstallShield声明有返回值的函数
- 有返回值的函数调用
- 有参数无返回值的函数
- JaveScript之有返回值的函数
- Castle AOP 系列(二):对接口方法调用的拦截(有源码)
- Castle AOP 系列(一):对类方法调用的拦截(有源码)
- C++基础---无返回值函数(void函数)
- OGRE+CEGUI游戏教程(3)----角色创建
- 安卓Doubango架构的使用小结
- POJ 3126的教训
- [帧间编码]概分法帧间编码快速判定
- Delphi列表框组件(TListBox)使用实例
- aop (!void,用来拦截有返回值的函数)
- C# Vector定义、operator 使用c#操作符重载
- 新手管理数据库需要了解的方面
- 自然语言处理(NLP)网上资源整理
- 词法分析资料
- 随心所欲 各取所需
- (C#)Winform中MD5加密
- 笔记:Gof设计模式--Observer
- 堆和栈区别