AOP 使用接口的前置通知

 Log.java

package com.atstudy.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;
/**
 * 该AOP由于实现了接口,很大概率Spring使用JDK动态代理
 */
public class Log implements MethodBeforeAdvice
{

    /**
     *
     * @param method  被调用方法的对象
     * @param args 被调用方法的参数
     * @param target 被调用方法的目标对象---实际对象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {

        System.out.println("调用了"+target.getClass().getName()+"的"+method.getName()+"方法...");

    }

}

spring.xml配置文件

<?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 id="userDao" class="com.atstudy.dao.impl.UserDaoImpl"/>

    <bean id="userService" class="com.atstudy.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <bean id="controller" class="com.atstudy.controller.UserController">
        <property name="userService" ref="userService"></property>
    </bean>
    <!--上面是自动注入-->


    <!--log负责通知的类代码-->
    <bean id="log" class="com.atstudy.log.Log"></bean>

    <!--配置AOP-->
    <aop:config>
        <!--
            配置切入点
            expression表示指定从当前工程的那个地方进行切入完成AOP
            execution()配置切入点表达式
            execution(* com.atstudy.controller.*.*(..))
                第一个*星号表示所有的返回值类型
                第二个*星号表示controller包中的所有的类
                第三个*星号表示controller包中所有类的所有方法名
                ..两点表示所有方法中的所有的参数类型以及参数个数,注意只能是两点
        -->
        <aop:pointcut id="ponintcut" expression="execution(* com.atstudy.controller.*.*(..))"/>
        <!--配置通知,advisor表示使用通知,advice-ref使用那个业务作为通知,pointcut-ref该通知的切入点-->
        <aop:advisor advice-ref="log" pointcut-ref="ponintcut"></aop:advisor>
    </aop:config>

</beans>

测试代码块

UserControllerTesting.java

package com.atstudy.test;

import com.atstudy.controller.UserController;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserControllerTesting
{

    public static void main(String[] args) {

        BeanFactory  beanFactory = new ClassPathXmlApplicationContext("spring.xml");
        UserController userController = (UserController) beanFactory.getBean("controller");
        userController.query();

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一剑封喉の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值