使用BeanPostProcessor方式实现Spring的AOP

1.IUserService.java

package com.zy.aop07;

import java.util.List;

public interface IUserService {

    /**

    * 获取所有的用户对象列表

    * @return

    */

    List getAllUser();

    /**

    * 保存用户

    * @param user

    * @return

    */

    boolean saveUser(Object user);

    /**

    * 根据用户uid删除该uid对应的用户信息

    * @param uid

    * @return

    */

    boolean deleteUser(int uid);

    /**

    * 更新指定用户信息

    * @param obj

    * @return

    */

    boolean updateUser(Object obj);

    void getUserByUid();

}

2.UserServiceImpl.java

package com.zy.aop07;

import org.springframework.stereotype.Component;

import java.util.ArrayList;

import java.util.List;

@Component("us")

public class UserServiceImpl implements IUserService {

    @Override

    public List getAllUser() {

        System.out.println("--------getAllUser----------");

        return new ArrayList<>();

    }

    @Override

    public boolean saveUser(Object user) {

        System.out.println("--------saveUser----------");

        return true;

    }

    @Override

    public boolean deleteUser(int uid) {

        System.out.println("--------deleteUser----------");

        return false;

    }

    @Override

    public boolean updateUser(Object obj) {

        System.out.println("--------updateUser----------");

        return true;

    }

    @Override

    public void getUserByUid() {

        System.out.println("--------getUserByUid----------");

        System.out.println(1 / 0);

        String str = null;

        System.out.println(str.length());

    }

}

3.MyAspect.java

package com.zy.aop07;

/**

* 自定义切面类,分别有前置方法和后置方法

*/

public class MyAspect {

    public void before(){

        System.out.println("this is my aspect before");

    }

    public void after(){

        System.out.println("this is my aspect after");

    }

}

4.beans.xml

      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">

   

   

   

   

   

5.MyBeanPostProcessor.java

package com.zy.aop07;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

/**

* 配置了包扫描之后,该类会初始化两个对象EventListenerMethodProcessor和DefaultEventListenerFactory,再外加我们自己的组件对象

*

* 所以会发现有三个before打印

*

* 我还专门加了一个UserServiceImpl2,你会发现将有四个before的打印(注意看看该类上的Component注解是否启用)

*

*/

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        System.out.println("this is before " + bean);

        return bean;

    }

    @Override

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        System.out.println("this is after");

        return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {

            @Override

            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                MyAspect ma = new MyAspect();

                ma.before();

                Object obj = method.invoke(bean, args);

                ma.after();

                return obj;

            }

        });

    }

}

6.TestAOP07.java

package com.zy.aop07;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAOP07 {

    @Test

    public void testAOP07(){

        ApplicationContext ac = new ClassPathXmlApplicationContext("com/zy/aop07/beans.xml");

        IUserService us = ac.getBean("us", IUserService.class);

        us.deleteUser(1);

        us.updateUser("zhangsan");

        us.saveUser("lisi");

        us.getAllUser();

        us.getUserByUid();

    }

}

你可能感兴趣的:(使用BeanPostProcessor方式实现Spring的AOP)