Spring AOP的使用

首先,需要导入一个jar包。

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

第二,使用AOP导入jar包就可以用了,不像使用注解开发那样还需要导入约束。

第三,使用。可以使用配置文件来配置,也可以使用注解来配置。

第四,使用xml配置。

          这里用的是自定义一个普通的类,里边写一些增强方法,用于在目标方法执行前后去做增强。

          而不是创建一个类去实现AfterReturningAdvice ,MethodBeforeAdvice..这些接口,然后去重写其方法,那样贼麻烦。

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--开启注解驱动-->
    <context:annotation-config/>
    <!--指定要扫描的包-->
    <context:component-scan base-package="com.huchuan"/>


    <!--配置AOP:需要导入AOP的约束-->
    <aop:config>
        <!--diyBefore是自定义的增强方法类-->
        <aop:aspect ref="diyBefore">
            <!--设置切入点,可以设置多个。这里表示切入点为com.huchuan.mapper包下的所有类的所有权限的方法-->
            <aop:pointcut id="pointcut1" expression="execution(* com.huchuan.mapper.UserMapperImpl.*(..))"/>

            <!--设置前置通知-->
            <aop:before method="beforeLog" pointcut-ref="pointcut1"/>
            <!--设置后置通知-->
            <aop:after method="afterLog" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>


</beans>

总结,AOP实际上就是用的代理模式,底层用了java的动态代理。我对AOP的理解就是在不该变原来代码的基础上做的一些对方法的增强的作用,本质上就是为了解耦,将业务层就只关心业务。而且使用AOP还对程序的维护,修改减轻了压力。一定程度上减少代码量。

下边是我自己做的一个动态代理,的实例。

1,先创建一个接口类

public interface UserMapper {
    int addUser(User user);
    int deleteUser(int id);
    int updateUser(User user);
    List<User> userList();
}

2,接口实现类

public class UserMapperImpl implements UserMapper{
    @Override
    public int addUser(User user) {
        System.out.println("增加了一个用户!");
        return 0;
    }

    @Override
    public int deleteUser(int id) {
        System.out.println("删除了一个用户!");

        return 0;
    }

    @Override
    public int updateUser(User user) {
        System.out.println("修改了一个用户!");
        return 0;
    }

    @Override
    public List<User> userList() {
        System.out.println("查询了一个(多个)用户!");
        return null;
    }
}

3,动态创建代理的类

public class MyProxy implements InvocationHandler {
    // 被代理的对象
    private Object target;

    public void setProxy(Object target) {
        this.target = target;
    }

    /**
     * 生成得到代理类
     */
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    /**
     *处理代理的实例,就是在这个方法中做被代理类执行方法的增强。并返回结果
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        // 方法执行前,需要做的事情。比如打印日志等
        System.out.println(method.getDeclaringClass().getName()+" 类的->"+method.getName()+"方法执行了!");

        // 执行代理类的方法
        Object invoke = method.invoke(target, args);

        // 方法执行后
        System.out.println(method.getDeclaringClass().getName()+" 类的->"+method.getName()+"方法完成了!");

        return invoke;
    }
}

4,测试

    @Test
    public void test1(){
        // 真实角色,即被代理类对象
        UserMapperImpl userMapper =new UserMapperImpl();

        // 代理角色,不存在。
        MyProxy myProxy = new MyProxy();

        myProxy.setProxy(userMapper);

        UserMapper proxy =(UserMapper) myProxy.getProxy();
        proxy.deleteUser(1);
    }

5,测试结果

 需要注意的是,测试类通过调 动态创建代理类的返回的结果是一个接口对象,不能强转为该接口的实现类,因为在使用向下转型的时候必须先使用向上转型,这是多态的知识。如果没先使用向上转型的话,运行的时候会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

soutv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值