spring-aop(通过接口实现,不推荐)

使用aop需要导入的依赖

<!--        使用aop需要导入的依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

使用spring的接口实现aop

接口MethodBeforeAdvice表示在原有的方法之前执行该方法
package com.kuang.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

/**
 * 接口MethodBeforeAdvice
 * 表示在目前对象之前实现该方法
 */
public class Log implements MethodBeforeAdvice {
    /**
     *method:要执行的目标对象的方法
     * args:参数
     * target:目标对象
     */
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName()+"的方法被执行了");
    }
}
接口AfterReturningAdvice:表示在原有的方法之后执行该方法
package com.kuang.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    /**
     * @param returnValue:返回值
     * @param method
     * @param args
     * @param target
     * @throws Throwable
     */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了" + method.getName() + "的方法;返回结果为:" + returnValue);
    }
}

接口

package com.kuang.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

实现类

package com.kuang.service;
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("实现了增加方法");
    }
    public void delete() {
        System.out.println("实现了删除方法");
    }
    public void update() {
        System.out.println("实现了修改方法");

    }
    public void select() {
        System.out.println("实现了查询方法");
    }
}

applicationContext.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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.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>
    <bean id="log" class="com.kuang.log.Log"></bean>
    <bean id="afterLog" class="com.kuang.log.AfterLog"></bean>

<!--    配置aop需要导入aop的约束-->

    <aop:config>
<!--        配置切入点,就是需要在哪里插入这个方法
            expression:表达式
            execution(1 2 3 4 5)
            1:修饰词
            2:返回值
            3:类名
            4:方法名
            5:参数
-->
        <aop:pointcut id="expression" expression="execution
        (* com.kuang.service.UserServiceImpl.*(..))"/>

<!--        执行环绕增强,要插入在哪个位置-->
        <aop:advisor advice-ref="log" pointcut-ref="expression"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="expression"/>
    </aop:config>
</beans>

 

测试类

import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void t(){
        ApplicationContext context = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理使用的是接口,必须要拿接口接受
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

 

...




posted @ 2021-11-26 11:53  江南0o0  阅读(104)  评论(0)    收藏  举报