学习Spring AOP

AOP(Aspect Oriented Program)面向切面编程;

1、理解Spring AOP 的基本术语;
切面(Aspect):切面是对象操作过程中的截面,通俗的说,事务、日志、安全性的框架、权限等就是切面。

通知(Advice):通知是某个切入点被横切后,所采取的处理逻辑,一般来说,切面中的方法就是通知。

切入点(Pointcut):切面注入到程序中的位置,只有符合切入点的条件,才能让通知和目标方法结合在一起。(自己理解为目标类中的所有方法的位置(前、后、环绕)都可以成为切入点)

连接点(Join Point):对象操作过程中的某个阶段点,它实际上是对象的一个操作。
2、AOP的意义
a、在开发过程中,日志、权限、安全性框架、目标方法完全是松耦合的;
b、在形成代理对象的方法的过程中就把这几个结合在一起了。
配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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-2.5.xsd">
<bean id='persondao' class='com.xkang.dao.impl.PersonDao'></bean>
<bean id='personService' class='com.xkang.seevice.impl.Personservice'>
    <property name='personDao' ref="persondao">

    </property>

</bean>
<bean id='logger' class='com.xkang.aspect.Logger'></bean>
<!--切面配置-->  
<aop:config>
<!-- 切入点配置  expression 切入点表达式  指明com.xkang.seevice.impl.Personservice类下的所有方法-->
    <aop:pointcut expression="execution(* com.xkang.seevice.impl.Personservice.*(..))" id="perform"/>
    <aop:aspect ref="logger"><!-- ref 指定logger为切面 -->
        <!-- 环绕通知    method 切面中的方法  pointcut-ref指向切面 -->
        <aop:around method="checkOut" pointcut-ref="perform"/>
    </aop:aspect>
    <aop:aspect ref="logger">

        <aop:after-throwing method="logging" pointcut-ref="perform" throwing="ex"/>
    </aop:aspect>
</aop:config>
 </beans>   

目标类:

package com.xkang.seevice.impl;
import com.xkang.dao.impl.PersonDao;
public class Personservice {
    private PersonDao personDao;
    public PersonDao getPersonDao() {
        return personDao;
    }
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    public String  savePerson(){

        personDao.savePerson();
        return "aaa";
    }
}

切面类:

package com.xkang.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {
    public void logging(Throwable ex){

        System.out.println(ex.getMessage());

    }
    public void checkOut(ProceedingJoinPoint joinpoint) throws Throwable{
        //Object obj = null;
        System.out.println("环绕通知开始了");
        joinpoint.proceed();
        System.out.println("环绕通知结束了");

    }
}

测试方法:

@Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Personservice wife=(Personservice)context.getBean("personService");
        String name = wife.savePerson();
        System.out.println(name);   
    }

程序运行结果:

环绕通知开始了
save Person
环绕通知结束了
null

将切面类改为:

package com.xkang.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {
    public void logging(Throwable ex){

        System.out.println(ex.getMessage());    
    }
    public Object checkOut(ProceedingJoinPoint joinpoint) throws Throwable{
        Object obj = null;
        System.out.println("环绕通知开始了");
        obj = joinpoint.proceed();
        System.out.println("环绕通知结束了");
        //System.out.println(obj);
        return obj;
    }
}

程序运行结果:

环绕通知开始了
save Person
环绕通知结束了
aaa

说明:
AOP中,context.getBean()方法得到的是一个代理对象,并没有得到一个真正对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值