Learning spring framework (III) - AOP (Aspect Oriented Programming)

That AOP Aspect Oriented Program Oriented Programming
  First, Aspect Oriented Programming thoughts inside, the functions are divided into core business functions , and peripheral functions .
    The so-called core business, such as landing, increase data, delete data called core business of
    so-called peripheral functions, such as performance statistics, logs, transaction management, etc.

  peripheral functions in the Spring aspect-oriented programming AOP idea where has been defined as cut

  in Aspect oriented programming AOP thoughts inside, cut core business functions and function are independently developed
  and then cut features and core business functions  "weaving" together, which is called AOP

Schematic

  1. Feature divided into two categories, accessibility and core business functions
  2. Accessibility and core business functions independently of each other to develop
  3. functions such as landing, even if there is no performance statistics and log output, it can also run normally
  4. If there is a need , they "log output" function and the "login" function  braided together, so that the landing time, you can see the log output
  5. the auxiliary function, also called section, which can be selectively, coupled to the lower section programming ideas and core business functions together, called a programming section

         

Preparing business class ProductService

 1 package com.how2java.service;
 2  
 3 public class ProductService {
 4      
 5     public void doSomeService(){
 6          
 7         System.out.println("doSomeService");
 8          
 9     }
10      
11 }

Prepare a test class TestSpring

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "springconfig.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

Prepare a log section LoggerAspect

package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
 
public class LoggerAspect {
 
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

Set up a profile springconfig.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.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--声明业务对象-->
<bean name="s" class="com.how2java.service.ProductService"/>
<!--声明日志切面-->
<bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
   <!--aop配置过程-->
<aop:config>
<aop:pointcut id="loggerCutpoint"
expression
="execution(* com.how2java.service.ProductService.*(..)) "/>
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>

aop configuration process to explain the meaning of each step: 

<aop:pointcut id="loggerCutpoint" expression="execution(* com.how2java.service.ProductService.*(..)) "/> 

This is a sound Mingqie point, id cut-off point called loggerCutPoint, used to mark the starting point, this expression says: meet later in the method of expression called, to carry out the operation section, similar to that triggered the section:

  1. The first * representative of return of any type: may be a void, int, string. . . . Specific determined according to the return type of call

  2. com.how2java.service.ProductService * (..):. Represents package called com.how2java.service under ProductService any of the methods of the class (where the wildcard * represents any of the methods, (..) a parameter indicative of the method any number and type)

It simply is, as long as any com.how2java.service this package ProductService class of a function is called, no matter what your return value that will trigger the switch, I would have to carry out section, which is the auxiliary function. For ProductService class only doSomeService () method of a function, * com.how2java.service.ProductService. * (..) can also be written  void com.how2java.service.ProductService.doSomeService () . But what accessibility is, that is the following three:

<aop:aspect id="logAspect" ref="loggerAspect">
  <aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

这三句是定义了一个切面,上面说只要触发开关,就会去执行切面,就是指的这里的切面,所谓切面,就是一个类中的方法而已,在本案例中就是指  loggerAspect类中的log()方法,id="logAspect"  表示切面的id,ref="loggerAspect"指明切面所属的类,pointcut-ref="loggerCutpoint" 指明触发切面的切点id,method="log" 指明执行切面时所用的方法名。

测试类运行的结果如下

------------------------------------------------------

需要注意的是,一个切点也可以设置多个切面,我们重新创建了两个loggerAspect2和loggerAspect3,如下

配置文件改为

<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
   <bean name="s" class="com.how2java.service.ProductService"/> <bean id="loggerAspect2" class="com.how2java.aspect.LoggerAspect2"/> <bean id="loggerAspect3" class="com.how2java.aspect.LoggerAspect3"/> <aop:config> <aop:pointcut id="loggerCutpoint" expression="execution(void com.how2java.service.ProductService.doSomeService()) "/>
     <aop:aspect id="logAspect2" ref="loggerAspect2"> <aop:before pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> <aop:aspect id="logAspect3" ref="loggerAspect3"> <aop:after pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> </aop:config> </beans>

运行结果为

 ------------------------------------------------------

 AOP的过程分为两步:1,在业务类中插入切点,2,将切点和切面类关联起来

 业务类就是核心类,就是网站的主要功能,切面就是辅助功能,日志,统计之类的

通过配置,可以实现,在某个方法调用的时候,触发别的方法执行,就好像在监视目标方法,你被执行,就触发我执行。

 

下面简单介绍一下通知:

  通知定义了切面要完成的工作内容和何时完成工作,就是什么时候去做辅助功能,功能具体是什么代码

  五种类型

    1. Before——在方法调用之前调用通知

    2. After——在方法完成之后调用通知,无论方法执行成功与否

    3. After-returning——在方法执行成功之后调用通知

    4. After-throwing——在方法抛出异常后进行通知

    5. Around——通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为

   前四个好理解的,最后一个around 表示切面在被监视的函数运行前后都会执行,下面是切面要执行的函数 log,log函数有一个形参 joinPoint 这个可以理解为断点,中间一句代表的就是被监视的程序运行,在被监视的程序运行时,可以替换他的形参,这个是 around 厉害的地方,如果被监视的程序,运行的时候输入的是一个haha字符串作为实参,但是经过log方法之后,这个参数就被替换为abc了

public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("我在被监视程序之前。。。");
        Object object = joinPoint.proceed(new Object[]{"abc"});
        System.out.println("我在被监视程序之后。。。" );
        return object;
    }

注:关于joinPoint的详细介绍,请点击此处查看

 

Guess you like

Origin www.cnblogs.com/churujianghudezai/p/11805670.html