Spring总结之AOP

一、Spring AOP简介(百度百科)

         面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring

框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度

降低,提高程序的可重用性,同时提高了开发的效率。

主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

二、Spring AOP实例

(1,前置通知;2,后置通知;3,环绕通知;4,返回通知;5,异常通知;)

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    <!-- 业务逻辑bean -->  
    <bean id="userService" class="top.ruandb.service.impl.UserServiceImpl" ></bean>
    <!-- 自定义的切面 -->
    <bean id="userServiceAspect" class="top.ruandb.advice.UserServiceAspect"></bean>
    <!-- AOP配置 -->
    <aop:config>
        <!-- 定义切面 -->
        <aop:aspect id="userServiceAspect" ref="userServiceAspect">
            <!-- 定义切点,表达式: -->
            <aop:pointcut expression="execution(* top.ruandb.service.*.*(..))" id="businessService"/>
            <!-- 前置通知,在方法执行之前通知 -->
            <aop:before method="doBefore" pointcut-ref="businessService"/>
            <!-- 后置通知:在方法执行之后通知 -->
            <aop:after method="doAfter" pointcut-ref="businessService"/>
            <!-- 环绕通知:在方法前后环绕 -->
            <aop:around method="doAround" pointcut-ref="businessService"/>
            <!-- 返回通知:方法返回后通知 -->
            <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
            <!-- 异常通知:出现异常后通知 -->
            <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
        </aop:aspect>
    </aop:config>   
    
</beans>
//切面
public class UserServiceAspect {
 
    public void doBefore(JoinPoint jp) {
        System.out.println(jp.getTarget().getClass().getName()+"前置通知:添加用户之前");
    }
    public void doAfter(JoinPoint jp) {
        System.out.println(jp.getTarget().getClass().getName()+"后置通知:添加用户之后");
    }
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕前");
        Object obj = pjp.proceed();
        System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕后");
        return obj;
    }
    public void doAfterReturning(JoinPoint jp) {
        System.out.println(jp.getTarget().getClass().getName()+"返回通知:返回通知");
    }
    public void doAfterThrowing(JoinPoint jp,Throwable ex) {
        System.out.println(jp.getTarget().getClass().getName()+"异常通知:程序异常了"+ex.getMessage());
    }  
}
 
public class SpringTest {
     ApplicationContext ac ;
     @Before
     public void setUp() {
         ac = new ClassPathXmlApplicationContext("applicationContext.xml");
     }
     @After
     public void tearDown() {
         ac = null;
     }
      
     @Test
     public void test1() {
        UserServiceI userService = (UserServiceI) ac.getBean("userService") ;
        userService.addUser("rdb");
     
     }
}
 
结果:
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl返回通知:返回通知
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕后
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后
 
结果(异常):
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl异常通知:程序异常了/ by zero
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后

猜你喜欢

转载自www.cnblogs.com/jnba/p/10832697.html