AOP(面向切面編程)


 

11、AOP

11.1、什么是AOP

 

11.2、Aop在Spring中的作用

提供聲明式事務,允許用戶自定義切面

 

 

11.3、使用Spring實現Aop

【重點】使用AOP注入,需要導入一個依賴包!

        <dependency>
           <groupId>org.aspectj</groupId>
           <artifactId>aspectjweaver</artifactId>
           <version>1.8.4</version>
       </dependency>

 

方式一:使用Spring的API接口【主要SpringAPI接口實現】

<?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   -->
   <bean id="userService" class="com.kuang.service.UserServiceImpl"/>
   <bean id="log" class="com.kuang.log.Log"/>
<!--   <bean id="afterLog" class="com.kuang.log.AfterLog"/>-->

<!--   方式一:使用原生的Spring API接口-->
<!--   配置aop:需要導入aop的約束-->
   <aop:config>
<!--       切入點: expression:表達式; execution(要執行的位置!* * * * *) -->
       <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--       執行環繞增加-->
       <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
       <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>

   </aop:config>
</beans>
public class MyTest {
   public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       //動態代理代理的使接口
       UserService userService = (UserService) context.getBean("userService");
       userService.add();
  }
}
public class Log implements MethodBeforeAdvice {

   //method:要執行的目標對象的方法
   //object:參數
   public void before(Method method, Object[] args, Object target) throws Throwable {
       System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了");
  }
}
public class AfterLog implements AfterReturningAdvice {
   //returnValue 返回值
   public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
//       returnValue="榮哥";
       System.out.println("執行了"+method.getName()+"返回結果為:"+returnValue);
  }
}

方式二:自定義實現AOP【主要是切面定義】

<!--   方式二:自定義類-->
   <bean id="diy" class="com.kuang.diy.DiyPoint"/>
   <aop:config>
<!--       自定義切面,ref 要引用的類-->
       <aop:aspect ref="diy">
<!--           切入點-->
           <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--           通知-->
           <aop:before method="before" pointcut-ref="point"/>
           <aop:after method="after" pointcut-ref="point"/>

       </aop:aspect>
   </aop:config>

方式三:使用注解實現

@Aspect  //標注這個類是一個切面
@Component
public class AnnotationPointCut {

   @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
   public void before(){
       System.out.println("=====方法執行前=====");
  }
   @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
   public void s(){
       System.out.println("=====方法執行后=====");
  }
   @Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
   public void around(){

  }
}
<!--   開啟注解支持!   JDK(默認 proxy-target-class="false")   cglib(proxy-target-class="true")-->
   <aop:aspectj-autoproxy proxy-target-class="false"/>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM