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