Spring课堂练习5

2023-10-04 23:10
文章标签 spring 课堂练习

本文主要是介绍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 {@Autowiredprivate 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;@Beforepublic void init() {// 基于Spring配置文件创建应用容器context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");}@Testpublic void testBraveKnight() {// 根据名称从应用容器里获取勇敢骑士对象BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");// 勇敢骑士执行任务braveKnight.embarkOnQuest();}@Testpublic void  testDamselRescuingKnight(){//根据救美骑士类从应用容器里获取救美骑士对象net.lhf.spring.lesson05.aop_xml.DamselRescuingKnightdamselRescuingKnight =context.getBean(DamselRescuingKnight.class);damselRescuingKnight.embarkOnQuest();}@Afterpublic 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;@Beforepublic void init() {// 基于Spring配置文件创建应用容器context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");}@Testpublic void testBraveKnight() {// 根据名称从应用容器里获取勇敢骑士对象net.lhf.spring.lesson05.aop_xml.BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");// 勇敢骑士执行任务braveKnight.embarkOnQuest();}@Testpublic void  testDamselRescuingKnight(){//根据救美骑士类从应用容器里获取救美骑士对象net.lhf.spring.lesson05.aop_xml.DamselRescuingKnightdamselRescuingKnight =context.getBean(DamselRescuingKnight.class);damselRescuingKnight.embarkOnQuest();}@Afterpublic 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;@Beforepublic void init() {// 基于Spring配置类创建应用容器context = new AnnotationConfigApplicationContext(AopConfig.class);}@Testpublic void testBraveKnight() {// 根据名称从应用容器里获取勇敢骑士对象BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");// 勇敢骑士执行任务braveKnight.embarkOnQuest();}@Testpublic void testDamselRescuingKnight(){//根据名称从应用容器里获取救美骑士对象DamselRescuingKnight knight = (DamselRescuingKnight) context.getBean("damselRescuingKnight");//救美骑士执行任务knight.embarkOnQuest();}@Afterpublic void destroy() {// 关闭应用容器context.close();}
}

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

这篇关于Spring课堂练习5的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/2908

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

SpringBoot 中 CommandLineRunner的作用示例详解

《SpringBoot中CommandLineRunner的作用示例详解》SpringBoot提供的一种简单的实现方案就是添加一个model并实现CommandLineRunner接口,实现功能的... 目录1、CommandLineRunnerSpringBoot中CommandLineRunner的作用

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot