您的位置:首页 > 其它

原创文章:卷共享和SAN系统的实现原理与应用价值

2012-03-05 00:02 302 查看
AOP基本概念学习:
Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对事物特征的抽象,而切面是横切性关注点的抽象。

joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,而且spring只支持方法类型的连接点,实际上joinpoint还可以是field或者类构造器

pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义

advice(通知):所谓的通知是指拦截到joinpoint之后所要做的事情。分为前置通知,后置通知,异常通知,最终通知,环绕通知

Target(目标对象):被代理的对象

Wcave(织入):指将aspects应用到target对象并导致proxy对象创建的过程

Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地增加一些方法或者field

代码一:接口以及实现类,
package cn.itcast.server;
public interface IPersonService {
public void save(String name);
public String getPersonName(Integer id);
public void update(String name,Integer id);
}

package cn.itcast.server.impl;
import cn.itcast.server.IPersonService;
public class PersonServiceBean implements IPersonService {
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方法");
}
}


代码二:定义的切面
package cn.itcast.server;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 定义切面
*/
@Aspect
public class MyInteceptor {
//定义切入点,换个说法就是定义要被拦截的方法
@Pointcut("execution(* cn.itcast.server.impl.PersonServiceBean.*(..) )")
private void anyMethod(){}//定义切入点的名称

//定义前置通知
@Before("anyMethod() && args(name)")//这里的args(name)是说明需要拦截的方法必须是带有一个参数的方法
public void doAccessCheck(String name){
System.out.println("这里输出的是前置通知   " + name);
}
//定义后置通知
@AfterReturning(pointcut="anyMethod()",returning="res")//这里的returning 是接收的被拦截方法的返回值
public void doAfterReturning( String res){
System.out.println("这里输出的是后置通知    " + res);
}
//定义最终通知
@After("anyMethod()")
public void doAfterReturn(){
System.out.println("这里输出的是最终通知");
}
//定义例外通知
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e){
System.out.println("这里输出的是例外通知   "+ e);
}
//定义环绕通知
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
//判断用户是否具有权限
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
return result;
}
}

@Pointcut("execution(* cn.itcast.server.impl.PersonServiceBean.*(..) )") 解释:
execution所带参数的解释:
先看一个基本的参数【* cn.itcast.server.impl..*.*(..)】
1:第一个*表示,方法的返回类型,cn.itcast.server.impl 是你要拦截的哪个包下面的方法

2:impl后面的【..*】表示,包下面的任意类,如果想指定特定的类,直接像这样cn.itcast.server.impl.PersonServiceBean,这就是制定拦截这个类PersonServiceBean,

3:再后面的*(..)表示任意方法,括号里面的两个小点表示任意的参数类型,无论几个

代码三:配置文件:
<?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开发(基于注解方式进行AOP开发)  -->
<aop:aspectj-autoproxy/>
<bean id="myInteceptor" class="cn.itcast.server.MyInteceptor"></bean>
<bean id="personService" class="cn.itcast.server.impl.PersonServiceBean"></bean>

</beans>


代码四:测试类:
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.server.IPersonService;
public class SpringAOPTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void interceptorTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
IPersonService ipersonService = (IPersonService) ac.getBean("personService");
ipersonService.update("xxx",2);
ipersonService.save("xx");
ipersonService.getPersonName(2);
}
}

ipersonService.update("xxx",2);
ipersonService.save("xx");
ipersonService.getPersonName(2);,这三个方法当满足切入点的条件时,才会被拦截。

我们看到切入点的定义:@Pointcut("execution(* cn.itcast.server.impl.PersonServiceBean.*(..) )")

里面的表达式说明,要拦截的方法是任意返回类型,类PersonServiceBean下面的任意方法。

所以上面三个方法都会被拦截。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐