Spring课堂练习5

课堂练习
1、增加拯救少女任务类与拯救少女骑士类
在这里插入图片描述

package net.lj.spring.lesson05.aop_xml;


import org.springframework.stereotype.Component;

@Component
public class RescueDamselQuest {
    public void embark() {
        System.out.println("执行救美任务。");
    }
}

在这里插入图片描述
在这里插入图片描述

package net.lj.spring.lesson05.aop_xml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DamselRescuingKnight {
    @Autowired
    private RescueDamselQuest rescueDamselQuest;



    public void embarkOnQuest() {
        rescueDamselQuest.embark();
    }
}


在测试代码中创建testDamselRescuingKnight()方法。在这里插入图片描述

package net.lj.spring.lesson05.aop_xml;

import net.lhf.spring.lesson05.aop_xml.BraveKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

public class TestKnight {
    private ClassPathXmlApplicationContext context;

    @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();
    }
    @Test
    public void  testDamselRescuingKnight(){
        //根据救美骑士类从应用容器里获取救美骑士对象
        net.lhf.spring.lesson05.aop_xml.DamselRescuingKnight
                damselRescuingKnight =context.getBean(DamselRescuingKnight.class);
        damselRescuingKnight.embarkOnQuest();

    }


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

运行testDamselRescuingKnight(),结果:
在这里插入图片描述
创建aop_annotation子包,把aop_xml子包中的文件复制到aop_annotation子包中
在这里插入图片描述
在aop_annotation子包创建注解接口-Action
在这里插入图片描述

package net.lj.spring.lesson05.aop_annotation;

import java.lang.annotation.*;


@Target(ElementType.METHOD)//拦截目标  -方法
@Retention(RetentionPolicy.RUNTIME) //保持策略 -运行时
@Documented//注释文档
public @interface Action {
    String name();
}

创建游吟诗人类MinstreAspect

在这里插入图片描述

package net.lj.spring.lesson05.aop_annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component//交给Spring容器管理
public class MinstrelAspect {
    //注解声明切点
    @Pointcut("execution(* net.lhf.spring.lesson05..*.embarkOnQuest(..))")
    public void embark() {

    }
    //注解声明前置通知
    @Before("embark()")
    public void singBeforeQuest(JoinPoint joinPoint){
        System.out.println("啦啦啦,骑士出发啦!");

    }
    //注解声明后置通知
    @After("embark()")
    public void singAfterQuest(JoinPoint joinPoint){
        System.out.println("真棒啊,骑士完成了任务!");
    }
}

在aop_annotation子包里创建Spring配置类 - AopConfig在这里插入图片描述

package net.lj.spring.lesson05.aop_annotation;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration // 标明是Spring配置类
@ComponentScan("net.lhf.spring.lesson05.aop_annotation") // 组件扫描
@EnableAspectJAutoProxy // 开启Spring对ApectJ的支持
public class AopConfig {
}

在aop_annotation子包里创建测试类 - TestKnight
在这里插入图片描述
在这里插入图片描述

package net.lj.spring.lesson05.aop_annotation;

import net.lhf.spring.lesson05.aop_xml.BraveKnight;
import net.lhf.spring.lesson05.aop_xml.DamselRescuingKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestKnight {
    private ClassPathXmlApplicationContext context;

    @Before
    public void init() {
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
    }
    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器里获取勇敢骑士对象
        net.lhf.spring.lesson05.aop_xml.BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }
    @Test
    public void  testDamselRescuingKnight(){
        //根据救美骑士类从应用容器里获取救美骑士对象
        net.lhf.spring.lesson05.aop_xml.DamselRescuingKnight
                damselRescuingKnight =context.getBean(DamselRescuingKnight.class);
        damselRescuingKnight.embarkOnQuest();

    }

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

运行测试方法testBraveKnight(),查看效果
在这里插入图片描述
在按照老师来做的时候,代码出现错误,获取不到Mike
在这里插入图片描述
在这里插入图片描述

package net.lj.spring.lesson05.aop_annotation;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestKnight {
    private AnnotationConfigApplicationContext context;

    @Before
    public void init() {
        // 基于Spring配置类创建应用容器
        context = new AnnotationConfigApplicationContext(AopConfig.class);
    }

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


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

在测试程序里增加对拯救少女骑士的测试方法 - testDamselRescuingKnight(),运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值