spring 中使用动态代理(xml配置方式)

Aop在Spring中的作用

提供声明式事务;允许用户自定义切面。

 

一、配置xml实现

使用maven创建项目,下载依赖

<!--pom.xml-->
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.8</version>
    </dependency>
    <!--
    aspectjweaver - 在spring中使用动态代理
    https://mvnrepository.com/artifact/org.aspectj/aspectjweaver
    -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.6</version>
    </dependency>
</dependencies>

需求:在增删改查接口中实现前置日志功能

定义接口 - UserService

package com.lxc.service;

public interface UserService {
    public void query();
    public void delete();
    public void edit();
    public void add();
}

重写接口类 - UserServiceImp

package com.lxc.service;
public class UserServiceImp implements UserService{
    @Override
    public void query() {
        System.out.println("query");
    }
    @Override
    public void delete() {
        System.out.println("delete");
    }
    @Override
    public void edit() {
        System.out.println("edit");
    }
    @Override
    public void add() {
        System.out.println("add");
    }
}

 

Log类

编写两个,一个是方法执行之前日志记录,一个是方法执行之后日志记录

前置环绕:

需要实现 MethodBeforeAdvice -接口中的before方法,几个参数说明:
(1)method : 要执行的目标方法;
(2)args :传递给目标方法的参数;
(3)target:目标类。

before方法参数,底层都是反射机制,所以,可以获取到目标类的任何一个属性、方法等信息!

package com.lxc.service;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    // Method 要执行的目标方法,
    // args 参数
    // target 目标类的实例对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(method.getName()+"方法执行了");
    }
}

后置环绕:

需要实现 AfterReturningAdvice - 接口中的 afterReturning 方法,除了参数一,后边的额参数跟上边的一样,记录下参数一:

(1)returnValue : 目标方法的返回值

package com.lxc.service;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    // returnValue 方法的返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置环绕,执行了"+method.getName()+",返回的结果为:"+returnValue);
    }
}

在beans.xml中配置

(1)第一步:

注册bean;

(2)第二步:

导入AOP约束,具体配置如下。

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

    <!--spring中使用动态代理-->
    <!--第一步:注册bean-->
    <bean id="userServiceImp" class="com.lxc.service.UserServiceImp" />
    <bean id="beforelog" class="com.lxc.service.Log" />
    <bean id="afterlog" class="com.lxc.service.AfterLog" />

    <!--
    第二步:导入AOP的约束,
    xmlns:aop="http://www.springframework.org/schema/aop"
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd
    第三部:
    配置AOP
        1、<aop:point /> 定位切入点(在哪个地方执行)
        -> id 唯一标记
        -> "execution()" 要执行的位置
        -> * 任意
        -> * 类
        -> .* 类下的所有方法
        -> .. 可以有任意参数
        ->
        2、<aop:advisor /> 环绕:在哪个类中使用
        -> advice-ref: 执行哪一个环绕
        -> pointcut-ref: 引用哪个切入点
    -->
    <aop:config>
        <aop:pointcut id="userServiceImpPoint" expression="execution(* com.lxc.service.UserServiceImp.*(..))" />
        <!--执行环绕:把Log类切入到UserServiceImp类的方法下边 -->
        <aop:advisor advice-ref="beforelog" pointcut-ref="userServiceImpPoint" />
        <aop:advisor advice-ref="afterlog" pointcut-ref="userServiceImpPoint" />
    </aop:config>

</beans>

执行测试

下边注意类型,因为我们动态代理的是一类接口,所以类型应该是 UserService 类型。

import com.lxc.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = ctx.getBean("userServiceImp", UserService.class);
        userService.add();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值