>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 20989 个阅读者 刷新本主题
 * 贴子主题:  Spring MVC:切面的应用 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-02-04 23:59:44     消息  查看  搜索  好友  邮件  复制  引用

      
何为切面:个人理解为中断,也就是在原先的方法执行前或执行后插入一些事件。

先建立一个切面执行类:              
  1.       package com.freestyle.test.beans;
  2.       public   class  Minstrel {
  3.        public  void  singBeforeQuest (){
  4.      System.out.println( " BeforeQuest , GOGOGO!!");
  5.      }
  6.        public  void  singAfterQuest (){
  7.      System.out.println( " AfterQuest , Well Done!!");
  8.      }
  9.      }

  在xml里面加入切面配置:              

  1.        <?xml version= "1.0" encoding= "UTF-8" ?>
  2.       < beans  xmlns= "http://www.springframework.org/schema/beans"
  3.      xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"  xmlns:context= "http://www.springframework.org/schema/context"
  4.      xmlns:mvc= "http://www.springframework.org/schema/mvc"
  5.      xmlns:aop= "http://www.springframework.org/schema/aop"
  6.      xsi:schemaLocation=  "http://www.springframework.org/schema/beans
  7.        http://www.springframework.org/schema/beans/spring-beans.xsd
  8.               http://www.springframework.org/schema/context
  9.               http://www.springframework.org/schema/context/spring-context-4.1.xsd
  10.               http://www.springframework.org/schema/mvc        
  11.               http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  12.               http://www.springframework.org/schema/aop
  13.               http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
  14.      < bean  id= "knight"  class= "com.freestyle.test.beans.BraveKnight">
  15.      < constructor-arg  ref= "quest">
  16.      </ constructor-arg>
  17.      </ bean>
  18.      < bean  id= "quest"  class= "com.freestyle.test.beans.SlayDragonQuest" />
  19.      < bean  id= "minstrel"  class= "com.freestyle.test.beans.Minstrel" />
  20.      < aop:config>
  21.      < aop:aspect  ref= "minstrel" >
  22.          < aop:pointcut  id= "embark"  expression= "execution(* *.embarkOnQuest(..))" />
  23.          < aop:before  pointcut-ref= "embark"  method= "singBeforeQuest" />
  24.          < aop:after  pointcut-ref= "embark"  method= "singAfterQuest" />
  25.      </ aop:aspect>
  26.      </ aop:config>
  27.       </ beans>

   现在我们测试一下:              

  1.       import org.springframework.context.support.ClassPathXmlApplicationContext;
  2.       import com.freestyle.test.beans.Knight;
  3.       public   class  BraveKnightTest {
  4.        public  static  void  main (String[] args){
  5.      ClassPathXmlApplicationContext w_context= new ClassPathXmlApplicationContext( "spring-beans.xml");
  6.      Knight w_knight=(Knight) w_context.getBean( "knight");
  7.      w_knight.embarkOnQuest();
  8.      Knight w_knight1=(Knight) w_context.getBean( "knight");
  9.      w_knight1.embarkOnQuest();
  10.      System.out.println(w_knight1==w_knight);
  11.      }
  12.      }

  运行结果:


  BraveKnight  instance  created :com.freestyle.test.beans.BraveKnight@ 44a7bdab
BeforeQuest , GOGOGO!!
SlayDragonQuest  embark.
AfterQuest , Well Done!!
BeforeQuest , GOGOGO!!
SlayDragonQuest  embark.
AfterQuest , Well Done!!
true

----------------------------
原文链接:https://blog.csdn.net/rocklee/article/details/50461187

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-02-04 23:59:44 重新编辑]
  Java面向对象编程-->集合(上)
  JavaWeb开发-->使用过滤器
  JSP与Hibernate开发-->第一个helloapp应用
  Java网络编程-->通过JavaMail API收发邮件
  精通Spring-->通过Axios访问服务器
  Vue3开发-->组合(Composition)API
  Netty的粘包和拆包问题分析
  Redis为什么单线程能够支持高并发
  面试官问什么是Spring循环依赖,该如何回答?
  微服务架构模型
  拦截器不拦截静态资源的三种处理方法
  springMVC:HandlerInterceptor拦截器的使用
  Spring配置中bean元素的id和name属性的区别
  Spring MVC 通过@Value注解读取.properties文件中的内容
  Redis用作消息队列
  RabbitMQ的用途、原理以及配置
  一文详解微服务架构(好文值得细读)
  Spring Boot集成shiro ehcache(使用shiro的缓存管理)
  阿里面试官问我:如何用Redis设计秒杀系统?我的回答让他比起...
  spring-boot-plus V1.4.0发布 集成用户角色权限部门管理 - g...
  Java-CORBA
  更多...
 IPIP: 已设置保密
树形列表:   
[url=http://www.zhent.com]... nihaota 2022-10-28 20:01:11
1页 1条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。