原来的具体实现

有这样一个接口

@Repository
public interface UserService {
    User getUser();
    Integer addUser(User user);
    Integer updateUser(User user);
    Integer deleteUser(Integer id);
}

这是它对应的实现类

@Service
public class UserServiceImpl implements UserService {
    @Override
    public User getUser() {
        System.out.println("查询了一个用户");
        return new User("user001","8888");
    }

    @Override
    public Integer addUser(User user) {
        System.out.println("新增了一个用户");
        return 10;//为了好测试,看到返回结果,这里先固定写为10。
    }

    @Override
    public Integer updateUser(User user) {
        System.out.println("更新了一个用户");
        return 1;
    }

    @Override
    public Integer deleteUser(Integer id) {
        System.out.println("删除了一个用户");
        return 1;
    }
}

然后调用的时候通过实现类来进行调用

public class UserController {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        userService.addUser(new User());
    }
}

输出

【log】执行了新增方法!
新增了一个用户

这是原有的方式实现一个接口的实现和调用,然后使用新的方式开始对每一个接口增加日志记录的操作

使用Spring中AOP的思想

首先,需要加入以下的依赖

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

然后是spring的配置文件

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--注册bean-->
    <!--第三种方式:注解实现-->
    <bean id="annotationPointcut" class="com.origin.proxy.AnnotationPointcut"/>
    <bean id="userservice" class="com.origin.service.impl.UserServiceImpl"/>
    <aop:aspectj-autoproxy/>
</beans>

同理接口和实现类同上,然后是我们切入的Bean

@Aspect
public class AnnotationPointcut {

    @Before("execution(* com.origin.service.*.*(..))")
    public void before(){
        System.out.println("方法执行前!");
    }

    @After("execution(* com.origin.service.*.*(..))")
    public void after(){
        System.out.println("方法执行后!");
    }

    @Around("execution(* com.origin.service.*.*(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println(proceedingJoinPoint.getArgs()[0]);
        System.out.println("环绕前!");
        //执行目标方法proceed
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后!");
        System.out.println(proceed);
    }
}

如何调用

public class UserController {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = (UserService) context.getBean("userservice");
        userService.addUser(new User("qiyuan","9999"));
    }
}

输出

User{username='hanhan', password='909090'}
环绕前!
方法执行前!
新增了一个用户
环绕后!
10
方法执行后!