今天研究了一下aop,感觉很热啊...不知道就太土鳖的感觉,所以学spring的时候先学学这个看看....
先放个在网上找的demo,跑起来再说.方便以后自己回头看看
首先新建一个java project,导入几个必须的包,aop,logging,aspectjrt, aspectjwaver,cglib以及asm.
然后定义一个接口:
/**
* @version $Id: Person.java Exp $
* @author wuxing
* 2014年11月17日 上午11:42:16
*/
package com.xing.ioc;
public interface Person{
public void save(String name);
public void update(String name, Integer id);
public String getPersonName(Integer id);
}
定义一个相应的实现:
/**
* @version $Id: PersonServiceBean.java Exp $
* @author wuxing
* 2014年11月17日 下午12:18:59
*/
package com.xing.ioc;
public class PersonServiceBean implements Person{
@Override
public void save(String name) {
System.out.println("我是save方法");
}
@Override
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
@Override
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
}
定义一个切面:
/**
* @version $Id: MyInterceptor.java Exp $
* @author wuxing
* 2014年11月17日 下午12:20:01
*/
package com.xing.ioc;
import org.aspectj.lang.JoinPoint;
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;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyInterceptor {
@Pointcut("execution(* com.xing.ioc.PersonServiceBean.*(..))")
private void anyMethod(){}//定义一个切入点
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name);
System.out.println("前置通知");
}
@AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
}
@After("anyMethod()")
public void after(JoinPoint joinPoint){
String sign = joinPoint.getSignature().toString();
System.out.println(sign);
String args = joinPoint.getArgs().toString();
System.out.println(args);
PersonServiceBean target = (PersonServiceBean) joinPoint.getTarget();
target.update("x",1);
System.out.println(target);
System.out.println("最终通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("例外通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入环绕通知");
Object object = pjp.proceed();//执行该方法
System.out.println("退出方法");
return object;
}
}
然后是最关键的applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
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"
default-autowire="byType" >
<context:component-scan base-package="com.xing.ioc"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="personServiceBean" class="com.xing.ioc.PersonServiceBean"/>
<bean id="myInterceptor" class="com.xing.ioc.MyInterceptor"/>
</beans>
具体的@Before,@After,@Around等是切入的时间
Pointcut是切入的位置,joinpoint是切入的当前的对象.
大致就这样..具体代码具体分析.恩恩!!
这是我的测试代码:
/**
* @version $Id: TestIoc.java Exp $
* @author wuxing
* 2014年11月17日 上午11:47:09
*/
package com.xing.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoc {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonServiceBean bean = (PersonServiceBean) ac.getBean("personServiceBean");
bean.save("吴星");
}
}
结果如下:
中间那些是为了测试joinpoint是干嘛的...
后面终于知道是为了拿当前的对象什么的...
吴星
前置通知
进入环绕通知
我是save方法
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$MethodSignatureImpl@45852c8b
[Ljava.lang.Object;@2a2f7d55
我是update()方法
com.xing.ioc.PersonServiceBean@13cd6d16
最终通知
退出方法
后置通知