spring aop framework section - without modifying the source code to add new features

EDITORIAL

A long time ago to write notes in the proper way cloud notes, ready to give it up, migrate notes to be here. Article may have many shortcomings, please understand, welcome Gangster comments.

As used herein, the thing

  1. java
  2. ecplipse
  3. spring

aop Overview

aop java based on the principle of dynamic proxy mode, there are examples of dynamic proxies in the java folder

aop Add Package

aop achieved in addition to the spring package also need to add packet
link: https://pan.baidu.com/s/1acjKuNkqxdwdoz1hghy_wg
extraction code: tbko

aop simple example

Links: https://pan.baidu.com/s/1ycVitoJJ0tHjexycTOnSGw
extraction code: o48k

Some examples of the use of surround notification parameters

Links: https://pan.baidu.com/s/1rr-wNlNFaCnc4AMPgBeVUA
extraction code: 50jy

aop exemplary custom profile

<?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:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation=" 
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
">
   <!-- 创建1个业务类对象 -->
   <bean id="service" class="com.etc.service.UserServiceImpl">  
   </bean>
   <!-- 创建1个业务类对象 -->
   <bean id="service2" class="com.etc.service.UserServiceImpl2">
   </bean>
   <!-- 创建1个自定义通知类对象 --> 
   <bean id="advice" class="com.etc.advice.MyAdvice">  
   </bean>
   <!-- 配置aop的织入-->
   <aop:config>
      <aop:pointcut expression="execution(* com.etc.service.UserService.*og*(..))" id="pc"/>
      <aop:pointcut expression="execution(* com.etc.service.UserService.*eg*(..))" id="pc2"/>
      <aop:aspect ref="advice">     
         <aop:before method="before" pointcut-ref="pc" />         <!-- 配置id为pc切点的前置织入-->
         <aop:after method="after" pointcut-ref="pc2" />  <!-- 配置id为pc2切点的后置织入-->
         <!--    
         <aop:before method="before" pointcut-ref="pc" />       
         <aop:after method="after" pointcut-ref="pc" />         
         <aop:around method="around" pointcut-ref="pc"/>
          <aop:after-throwing method="afterthrow" pointcut-ref="pc"/>
           -->
      </aop:aspect>
   </aop:config>
</beans>

aop tags Introduction

<Aop: config> tag

  1. The label for the specific implementation of configuration aop weaving, which is written the aop
  2. The configuration in the root tag label

<aop: pointcut> tag
uses examples:

<aop:pointcut expression="execution(* com.etc.service.UserService.*og*(..))" id="pc"/>
  1. The tag is used to add aop tangent point, it is <aop: config> subtag, to the designated path for the method of adding the tangent point
  2. expression attribute: used to set the position of the tangent point, as fill format, there is a "before the package name " number, separated by a space and the package name, package names can be added at any position, " " to denote fuzzy query
  3. id attribute: setting a tangent point to the id, using the <aop: before> specifies the id of the tag tangent point position, etc.

<aop: aspect> tag
uses examples:

<aop:aspect ref="advice">     
      </aop:aspect>
  1. The label <aop: config> subtag with <aop: before> subtabs
  2. The tag is used to specify notification bean object, the tag has <aop: before> subtabs embodied inserted into the notification target cut point
  3. property ref: Custom notification to be inserted bean object id

<aop:before >、<aop:after >、<aop:around >、<aop:after-throwing >标签
使用示例:

<aop:after method="after" pointcut-ref="pc2" />
  1. <Aop: before />: pre-notification, before executing the method for performing the specified cut-point method
  2. <Aop: after />: post notification method executed after executing the specified cut-point method
  3. <Aop: around />: Surround notification, write statement is executed perform the method in the specified cut-point execution method
  4. <Aop: after-throwing />: Throws notice, after performing this method throws an exception operation program
  5. Other methods not three parameters, there is a notification method surround parameter ProceedingJoinPoint p
  6. If it is specified a method of front, rear and surround the notification, the first pre-execution notice, then execute surround notification, the notification performed last post
  7. method property: its fill aop: aspect parent tag specified in the notification method of a class name, said method designated tangent point insertion
  8. pointcut-ref attribute: Specifies a </ aop: aspect> tangent point id, method of performing the method specified in tangential point represents
  9. Only around advice can prevent the execution of the original code, to achieve additional rights management. Other announcements without this feature

The method of slicing performed around the notification

  • p.proceed (); perform slicing method can be achieved, and can be used repeatedly p.proceed () execution
  • p.getTarget () you can call the method of the acquisition target
  • if (p.getTarget () instanceof UserServiceImpl) {...} is determined p.getTarget () to obtain the object is not of class UserServiceImpl

p.proceed () Example:

   public void around(ProceedingJoinPoint p)
   {
      System.out.println("这是环绕前通知!");
      try {
         p.proceed();
      } catch (Throwable e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }//执行原有的业务代码
      System.out.println("这是环绕后通知!");
   }

p.getTarget()示例:
public void around(ProceedingJoinPoint p)
   {
      //只有角色是admin才能删除帖子
       if (p.getTarget() instanceof UserServiceImpl)
       {
          UserServiceImpl imp = (UserServiceImpl) p.getTarget();
          if(!"admin".equals(imp.getRole()))
          {
             System.out.println("只有管理员才能删除帖子!");
             return;
          }             
       }
       try {
            p.proceed();//执行原有的代码
         } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }//执行原有的业务代码
 
      /*周日禁止注册
      Calendar c = Calendar.getInstance(); //获取当前的日历
      int day = c.get(Calendar.DAY_OF_WEEK);
     
      if(day==Calendar.SUNDAY)
      {
         System.out.println("周四禁止注册!");
         return;
      }
      */
   }
Published 38 original articles · won praise 17 · views 1236

Guess you like

Origin blog.csdn.net/nineya_com/article/details/103533154