Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

一、面向切面编程AOP

  目标:让我们可以“专心做事”,避免繁杂重复的功能编码

  原理:将复杂的需求分解出不同方面,将公共功能集中解决

Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第1张图片

 

Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第2张图片

  *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态添加功能的技术。

 二、AOP相关术语

  1.增强处理(Advice)

    前置增强,后置增强,环绕增强、异常抛出增强、最终增强等。。。

  2.切入点(Pointcut)

  3.连接点(Join Point)

  4.切面(Aspect)

  5.目标对象(Target Object)

  6.AOP代理(AOP proxy)

  7.织入(Weaving)

三、使用Spring AOP实现日至输出

Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第3张图片

Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第4张图片

Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第5张图片

  1.在项目中添加Spring AOP的jar文件

 Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第6张图片

 

  2.编写前置增强和后置增强实现日志功能

package aop;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;

/**
 * 
 * @author TengYiCheng
 *
 */
public class UserServiceLogger {
    private static final Logger logger = Logger.getLogger(UserServiceLogger.class);
    /**
     * 代表前置增强的方法
     * @param jp
     */
    public void before(JoinPoint jp){
        logger.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法入参:"+Arrays.toString(jp.getArgs()));
    }
    /**
     * 代表后置增强的方法
     * @param jp
     * @param result
     */
    public void afterReturning(JoinPoint jp,Object result){
        logger.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法返回值:"+result);
    }
}

  3.编写Spring配置文件,对业务方法进行增强

 1 xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop
 8         http://www.springframework.org/schema/aop/spring-aop.xsd">
 9     
10     <bean id="dao" class="dao.impl.UserInfoDaoImpl">bean>
11     <bean id="service" class="service.impl.UserServiceImpl">
12         <property name="dao" ref="dao">property>
13     bean>
14     <bean id="theLogger" class="aop.UserServiceLogger">bean>
15     
16     <aop:config>
17         
18         <aop:pointcut id="pointcut" expression="execution(public int addUser(entity.UserInfo))"/>
19         
20         <aop:aspect ref="theLogger">
21             
22             <aop:before method="before" pointcut-ref="pointcut"/>
23             
24             <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
25         aop:aspect>
26     aop:config>
27 beans>

 

  4.编写代码获取带有增强处理的业务对象

 

package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import entity.UserInfo;
import service.IUserService;

/**
 * 
 * @author TengYiCheng
 *
 */
public class AOPLoggerTest {
    @Test
    public void aopTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserService service = (IUserService) context.getBean("service");
        UserInfo user = new UserInfo();
        user.setId(10010);
        user.setUserName("Test");
        user.setUserPassword("123456");
        user.setPhone("13201069939");
        service.addUser(user);
    }
}

 Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)_第7张图片

 

转载于:https://www.cnblogs.com/tengqiuyu/p/7676568.html

你可能感兴趣的:(Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming))