本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!