Spring-5 Spring AOP

采用配置方式使用AOP
(一)创建本讲所需子包
在net.hw.spring包里创建lesson05.aop_xml子包
在这里插入图片描述
创建杀龙任务类
在aop_xml子包里创建杀龙任务类 - SlayDragonQuest
在这里插入图片描述
创建勇敢骑士类
在aop_xml子包里创建勇敢骑士类 - BraveKnight
在这里插入图片描述
创建游吟诗人类
在aop_xml子包里创建游吟诗人类 - Minstrel

import org.springframework.stereotype.Component;

@Component
public class Minstrel {
    public void singBeforeQuest() {
        System.out.println("啦啦啦,骑士出发了!");
    }

    public void singAfterQuest() {
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

创建Spring配置文件
在resources里创建aop_xml目录,在里面创建spring-config.xml配置文件
在这里插入图片描述

<?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:context="http://www.springframework.org/schema/context"
       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/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:component-scan base-package="net.qyk.spring.lesson05.aop_xml" />

    <!--AOP配置-->
    <aop:config>
        <!--定义切面-->
        <aop:aspect ref="minstrel">
            <!--定义切点-->
            <aop:pointcut id="embark" expression="execution(* net.qyk.spring.lesson05..*.embarkOnQuest(..))" />
            <!--声明前置通知-->
            <aop:before method="singBeforeQuest" pointcut-ref="embark"/>
            <!--声明后置通知-->
            <aop:after method="singAfterQuest" pointcut-ref="embark" />
        </aop:aspect>
    </aop:config>

</beans>

在pom文件里添加AOP相关依赖

<!--Spring AOP-->                                         
<dependency>                                              
    <groupId>org.springframework</groupId>                
    <artifactId>spring-aop</artifactId>                   
    <version>${spring.version}</version>                  
</dependency>                                             
<!--AspectJ支持-->                                          
<dependency>                                              
    <groupId>aspectj</groupId>                            
    <artifactId>aspectjrt</artifactId>                    
    <version>1.5.4</version>                              
</dependency>                                             
<dependency>                                              
    <groupId>org.aspectj</groupId>                        
    <artifactId>aspectjweaver</artifactId>                
    <version>1.9.6</version>                              
    <scope>runtime</scope>                                
</dependency>     

创建测试类 - TestKnight
在test/java里创建net.hw.spring.lesson05.aop_xml包,在包里创建TestKnight

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import o
rg.springframework.context.support.ClassPathXmlApplicationContext;


public class TestKnight {

    private ClassPathXmlApplicationContext context; // 基于类路径XML配置文件的应用容器

    @Before
    public void init() {
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
    }

    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器中获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
        // 关闭应用容器
        context.close();
    }
}

运行测试方法testBraveKnight(),查看结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值