Spring框架----Spring的基于XML的AOP的实现

38 篇文章 1 订阅
23 篇文章 2 订阅

导入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-dbutils</groupId>
        <artifactId>commons-dbutils</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>t
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.9</version>
    </dependency>
</dependencies>

导入包org.aspectj它能够帮助我们解析切入点表达式
接口类

public interface IAccountService {
    /*模拟保存账户*/
    void saveAccount();
    /*模拟更新账户*/
    void updateAccount(int i);
    /*模拟删除用户*/
    int deleteAccount();
}

实现类

/*账户的业务层实现类*/
public class AccountServiceImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("执行了保存");
    }

    public void updateAccount(int i) {
        System.out.println("执行了更新"+i);
    }

    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

公共代码类

/*用于记录日志的工具类,它里面提供了公共的代码*/
public class Logger {
    /*用于打印日志,计划让其在切入点方法执行之前执行,(切入点方法就是业务层方法)*/
        * 用spring配置的方式如何去做呢?
    * */
    public void printLog(){
        System.out.println("Logger类中的printLog方法开始记录日志了");
    }
}

如何实现在切入点方法之前开始执行呢,我们只要创建AccountService接口的代理对象,然后再执行执行这些方法之前调用logger中的这个方法就行了

搜索xmlns: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"
       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">
</beans>

spring中基于XML的AOP配置步骤
1.把通知Bean也交给spring来管理
2.使用aop:config标签表名开始AOP的配置
3.使用aop:aspect标签表示配置切面

id属性是给切面提供一个唯一的标识
ref属性:是指定通知类bean的id
4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
我们现在示例是让printlog方法在切入点方法执行之前执行。所以是前置通知
aop:before标签表示配置前置通知。
method属性:用于指定logger类中哪个方法是前置通知。
pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层哪些方法增强
切入点表达式的写法
关键字:execution(表达式)
表达式:访问修饰符 返回值 包名.包名…类名.方法名(参数列表)

标准表达式写法
public void com.yujie.service.impl.AccountServiceImpl.saveAccount();
我们的切入点表达式明确的说明了这个方法要对哪个方法进行增强。

<?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"
       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">
    <!--配置spring的IOC,把service对象配置进来,它需要增强
    它想在执行它的任何一个方法都记录了日志
    -->
    <bean id="accountService" class="com.yujie.service.impl.AccountServiceImpl"></bean>

    <!--配置logger类
    这个日志类中有一个方法可以记录日志,我们就配置了一个日志的通知,并且说明了我们这个里面有printLog方法会在这个方法执行之前执行
    -->
    <bean id="logger" class="com.yujie.utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
        <!--配置切面,引用了上面的通知,通知中的方法pringLog表示先执行,后面切入点表达式表名要对saveAccount进行增强
        怎么增强就是之前的代理对象的写法
        -->
        <aop:aspect id="logAdvice" ref="logger">
            <!--配置通知的类型,并且建立通知方法-->
            <aop:before method="printLog" pointcut="execution(public void com.yujie.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值