Spring AOP横向开发实现

AOP

Spring框架的AOP机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代码。在业务流程执行过程中,Spring框架会根据业务流程要求,自动把独立编写的功能代码切入到流程的合适位置。有三种实现方式

在实现AOP之前首先配置AOP约束
约束配置
‘’’

       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
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

‘’’
再配置需要响应的Bean

一:使用Spring API接口

实现接口MethodBeforeAdvice , AfterReturningAdvice等

  public class BeforeLog implements MethodBeforeAdvice {
       //前置日志
       public void before(Method method, Object[] objects, Object o) throws Throwable {
           //method被执行的目标的方法 objects参数 o:目标对象
           System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
       }
   }
   public class AfterLog implements AfterReturningAdvice {
       //o:返回值
       public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
           System.out.println("执行了"+method.getName()+"方法,返回值为"+o);
       }
   }

配置AOP
‘’’

      <aop:config>
    <!--配置切入点.就是设置切入到什么地方-->
    <aop:pointcut id="point" expression="execution(* com.lss.pojo.UserServiceImpl.*(..))"/>
     <!--执行环绕,将设置的前置日志和后置日志切入到设置好的切入点中-->
    <aop:advisor advice-ref="before" pointcut-ref="point" />
    <aop:advisor advice-ref="after" pointcut-ref="point" />
</aop:config>  

‘’’

二:自定义切面

自定义类,当作前置日志和后置日志

public class DiyPoint {   
          public void before(){
              System.out.println("=====执行方法前=====");
          }
          public void after(){
              System.out.println("=====执行方法后=====");
          }
      }

aop配置,自定义切入点

<bean id="diypoint" class="com.lss.DIY2.DiyPoint" />
aop:config>
    <!--aspect:切面-->
    <aop:aspect ref="diypoint">
        <!-设置切入点-->
        <aop:pointcut id="diy" expression="execution(* com.lss.pojo.UserServiceImpl.*(..))"/>
        <aop:before method="before" pointcut-ref="diy" />
        <aop:after method="after" pointcut-ref="diy" />
    </aop:aspect>
</aop:config>

三:使用注解

自定义前置日志和后置日志等其他

@Aspect
   //注解该类为一个切面
   public class Auto {
   //execution(* com.lss.pojo.UserServiceImpl.*(..))切入点,即切入到什么地方
   @Before("execution(* com.lss.pojo.UserServiceImpl.*(..))")
   
   public void before(){
       System.out.println("执行前"); }
   @After("execution(* com.lss.pojo.UserServiceImpl.*(..))")
   public void after(){
       System.out.println("执行后"); }}'''
   再xml中开启注解支持,并注册Bean
   <bean id="auto" class="com.lss.Auto.Auto" />
   <aop:aspectj-autoproxy/>

统一测试方法

ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationConfig.xml");
       //动态代理代理的是接口
       UserService userService = (UserService) context.getBean("userService");
        //serService.add();测试方法是否输出前置和后置信息
        userService.delete();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值