Spring 面向切面(AOP)编程,注解

引入AOP命名空间

两种方式:
1. 基于XML配置方式进行AOP开发。
2. 基于注解方式进行AOP开发。


/**
 * Created by nanji on 2017/1/19.
 */
public class JDKProxyFactory implements InvocationHandler{
    private Object targetObject;
    public Object createProxyIntance(Object targetObject){
        this.targetObject=targetObject;
        return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),this.targetObject.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //环绕通知
        Object result=null;
        PersonServiceBean bean=(PersonServiceBean)this.targetObject;
        if(bean.getUser()!=null){
            //....advice() -->前置通知
            try {
                result=method.invoke(targetObject,args);
                // afteradvice () -->后置通知
            } catch (RuntimeException e){
                //exceptionAdvice() -->例外通知
            }finally {
                //finallyadvice() --嘴终通知
            }
        }
        return result;
    }
}

public class PersonServiceBean implements PersonService{
    @Override
    public void save(String name) {
        System.out.println("xxxxxxxx");
       throw new RuntimeException("wo is exception");
    }

    @Override
    public void update(String name, Integer id) {
        System.out.println("我是save()方法");
    }

    @Override
    public String getPersonName(Integer id) {
        return "xxxxsss ";
    }
}

@Aspect
public class MyInterceptor {
    @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
    private void anyMethod(){
        System.out.println("anyMethod.....");
    }


    @Before("anyMethod() && args(name)")
    public void doAccessCheck(String name){
        System.out.println("my is before "+name);
    }
    @AfterReturning(pointcut = "anyMethod()",returning = "result")
    public void doAfterReturning(String result){
        System.out.println("houzhitongzhi"+result);
    }

    @After("anyMethod()")
    public void doAfterCheck(){
        System.out.println("my is after");
    }

    @AfterThrowing(pointcut = "anyMethod()",throwing = "e")
    public void doAfterThrowing(Exception e ){
       // throw new RuntimeException("wo is exception ");
         System.out.println("exception informing "+e);
    }

    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{
        System.out.println("into method");
        Object result=pjp.proceed();
        System.out.println("out method");
        return result;
    }
}

public class SpringAOPTest {

    @Before
    public void before() throws Exception {
    }

    @After
    public void after() throws Exception {
    }

    @Test
    public void interceptorTest() {
        ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
        PersonService personService=(PersonService)cxt.getBean("personService");
        personService.save("xxxxxx");
        //personService.getPersonName(2);
    }

再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-2.5.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
    <aop:aspectj-autoproxy/>
    <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
    <aop:config>
        <aop:aspect id="asp" ref="aspetbean">
            <aop:pointcut id="mycut"  expression="execution(!void cn.itcast.service.impl.PersonServiceBean.*(..))" />
            <aop:before method="doAccessCheck" pointcut-ref="mycut"/>
            <aop:after-returning method="doAfterReturning" pointcut-ref="mycut"/>
            <aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut"/>
            <aop:after method="doAfterCheck" pointcut-ref="mycut"/>
            <aop:around method="anyMethod" pointcut-ref="mycut"/>
        </aop:aspect>
    </aop:config>
</beans>
import cn.itcast.service.PersonService;

/**
 * Created by nanji on 2017/1/20.
 */
public class PersonServiceBean implements PersonService{
    @Override
    public void save(String name) {
        System.out.println("xxxxxxxx");
    }

    @Override
    public void update(String name, Integer id) {
        System.out.println("我是save()方法");
    }

    @Override
    public String getPersonName(Integer id) {
        return "xxxxsss ";
    }
}

/**
 * Created by nanji on 2017/1/20.
 */
//@Aspect
public class MyInterceptor {

 //   @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
    private void anyMethod(){
        System.out.println("anyMethod.....");
    }


  //  @Before("anyMethod() && args(name)")
    public void doAccessCheck(){
        System.out.println("my is before ");
    }
   // @AfterReturning(pointcut = "anyMethod()",returning = "result")
    public void doAfterReturning(){
        System.out.println("houzhitongzhi");
    }

 //   @After("anyMethod()")
    public void doAfterCheck(){
        System.out.println("my is after");
    }

  //  @AfterThrowing(pointcut = "anyMethod()",throwing = "e")
    public void doAfterThrowing(){
       // throw new RuntimeException("wo is exception ");
         System.out.println("exception informing ");
    }

  //  @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{
        System.out.println("into method");
        Object result=pjp.proceed();
        System.out.println("out method");
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值