目标:掌握让spring创建代理对象,从Spring容器中手动的获取代理对象。

第一步:导入jar包

【核心4+1、AOP联盟(规范)、spring-aop(实现)】

jar包链接:链接: https://pan.baidu.com/s/11z96PkDfNGovwrDWTCqvbg 提取码: g6yq

第二步:建立一个目标类

2.1、首先建立一个目标类的接口IUserService

public interface IUserService {
    public void addUser();
    public void updateUser();
    public void deleteUser();
}

2.2、再建立一个目标类UserServiceImpl,实现接口中的方法

public class UserServiceImpl implements IUserService{
    @Override
    public void addUser() {
        System.out.println("添加用户");
    }

    @Override
    public void updateUser() {
        System.out.println("更新用户");
    }

    @Override
    public void deleteUser() {
        System.out.println("删除用户");
    }
}

2.3、建立一个切面类MyAspect

注意:导包的时候,注意MethodInterceptor不是cglib下的类,而是aopalliance下的包

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyAspect implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前......");
        Object obj = methodInvocation.proceed();
        System.out.println("后......");
        return obj;
    }
}

第三步:配置Spring

新建一个beans.xml文件放置在src资源目录下,看我上方的目录结构图!

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p ="http://www.springframework.org/schema/p"
       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/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
<!--业务类-->
<bean id="Service" class="com.xianzhou.service.Impl.UserServiceImpl"/>
<!--切面类-->
<bean id="myAspect" class="com.xianzhou.myaspect.MyAspect"/>
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="interfaces" value="com.xianzhou.service.IUserService"/>
    <property name="target" ref="Service"/>
    <property name="interceptorNames" value="myAspect"/>
    <property name="optimize" value="true"/>
</bean>
</beans>

第四步:建立一个测试类测试上述代码是否有问题:

可使用单元测试的方法或者直接在main里面测试,单元测试需要导入JUnit4的jar包

JUnit4包的链接:链接: https://pan.baidu.com/s/1ArOw391g8k8GwHvFnjWCPQ 提取码: a6uh

import com.xianzhou.service.IUserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopTest {
    @Test
    public void test1(){
        ApplicationContext context =new ClassPathXmlApplicationContext("beans.xml");
        IUserService proxyService = (IUserService) context.getBean("proxyService");
        proxyService.addUser();
        proxyService.deleteUser();
    }
}

第五步:测试结果如下