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


使用aop需要导入的依赖


        
            org.aspectj
            aspectjweaver
            1.9.4
        

使用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"?>
"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">


    "userService" class="com.kuang.service.UserServiceImpl">
    "log" class="com.kuang.log.Log">
    "afterLog" class="com.kuang.log.AfterLog">



    

        "expression" expression="execution
        (* com.kuang.service.UserServiceImpl.*(..))"/>


        ref="log" pointcut-ref="expression"/>
        ref="afterLog" pointcut-ref="expression"/>
    

测试类

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();
    }
}