spring的MethodInterceptor方法

前文学习了spring的MethodBeforeAdvice接口,需要重写before方法,然后在before方法里写额外功能代码,还有一种是实现MethodInterceptor接口,重写invoke方法,invoke方法中可以写额外功能代码,也能执行原始方法,具体看代码

1.首先新建一个arround类实现MethodInterceptor接口,并重写invoke方法

public class arround implements MethodInterceptor {


    /*
    * invoke方法的作用:额外功能书写在invoke方法
    *                 额外功能可以运行在原始方法之前,之后,或者前后都执行
    * 参数:MethodInvocation:指的是额外功能所增加给的那个原始方法
    * 额外功能加给了谁,invocation.proceed()就代表让哪个方法执行
    *
    * Object返回值代表的是原始方法执行后的返回值
    *
    * */

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("额外功能=============");
        Object proceed = (Object) invocation.proceed();//让原始方法运行
        return proceed;
    }
}

2.同样定义原始方法

public interface UserService1 {

    public void login(String name,String password);

    public void logout();

}
public class UserServiceImpl implements UserService1 {
    public void login(String name, String password) {
        System.out.println("login.method");
    }

    public void logout() {
        System.out.println("logout.method");
    }
}

3.编写spring配置文件

<bean id="userService1" class="proxy.UserServiceImpl"></bean>

<!--    <bean id="before" class="dynamic.Before"></bean>-->
    <bean id="around" class="dynamic.arround"></bean>

    <aop:config>
        <!--所有的方法都作为切入点,加入额外功能-->
        <aop:pointcut id="pc" expression="execution(* *(..))"/>
        <!--目的把切入点和额外功能进行整合-->
        <aop:advisor advice-ref="around" pointcut-ref="pc"></aop:advisor>
    </aop:config>

4.运行测试类,看运行结果

@Test
public void test14(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext5.xml");
    UserService1 userService = (UserService1) ctx.getBean("userService1");
    userService.login("wang","123");
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Spring AOP(面向切面编程)来监听某个方法的调用。具体步骤如下: 1. 创建一个类,实现SpringMethodInterceptor接口。 2. 在该类中实现intercept方法,该方法会在目标方法被调用时被执行。 3. 在配置文件中声明一个Advisor,用于将该类与目标方法绑定。 4. 在配置文件中声明一个ProxyBean,用于代理目标类。 下面是一个示例代码: ```java import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyMethodInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("Before method execution"); Object result = invocation.proceed(); System.out.println("After method execution"); return result; } } ``` 在配置文件中声明Advisor和ProxyBean: ```xml <bean id="myMethodInterceptor" class="com.example.MyMethodInterceptor" /> <bean id="targetBean" class="com.example.TargetClass" /> <bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="targetBean" /> <property name="interceptorNames"> <list> <value>myMethodInterceptor</value> </list> </property> </bean> <aop:config> <aop:advisor advice-ref="myMethodInterceptor" pointcut="execution(* com.example.TargetClass.targetMethod(..))" /> </aop:config> ``` 这样,当TargetClass类中的targetMethod方法被调用时,MyMethodInterceptor中的intercept方法会被执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值