软件151  王帅

1、目标对象的接口:IStudent.java

  package  com.dragon.study;
  public   interface  IStudent  {
     public   void  addStudent(String name);

2、目标类:StudentImpl.java

 package  com.dragon.study.Impl;
 import  com.dragon.study.IStudent;
public   class  StudentImpl  implements  IStudent {
  public   void  addStudent(String name) {
 System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );
     } 

3、前置通知:BeforeAdvice.java

 package  com.dragon.Advice;
  import  java.lang.reflect.Method;
  import  org.springframework.aop.MethodBeforeAdvice;
 public   class  BeforeAdvice  implements  MethodBeforeAdvice {
public   void  before(Method method,Object[] args, Object target)
 throws  Throwable {  
System.out.println( " 这是BeforeAdvice类的before方法. " );   
 } 

4、后置通知:AfterAdvice.java

package com.dragon.Advice;
 import java.lang.reflect.Method;
 import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice{
 public void afterReturning(Object returnValue ,Method method,
Object[] args,Object target) throws Throwable{
 System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}

5、环绕通知:CompareInterceptor.java

package com.dragon.Advice;
 import org.aopalliance.intercept.MethodInterceptor;
 import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor{
 public Object invoke(MethodInvocation invocation) throws Throwable{
 Object result = null;
String stu_name = invocation.getArguments()[].toString();
if ( stu_name.equals("dragon")){
   //如果学生是dragon时,执行目标方法,
 result= invocation.proceed();       
} else{
 System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
}
  return result;
   }
}

6、配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
 <bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 <bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 <bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor">
</bean>
 <bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dragon.study.IStudent</value>
 </property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>  
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
 </property>
</bean>
</beans>

7、测试:Test.java

package com;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 import com.dragon.study.IStudent;
public class Test {
 public static void main(String[] args) {
     ApplicationContext ctx = 
new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");    
    IStudent person = (IStudent)ctx.getBean("student");
   person.addStudent("dragon");
  }
}

spring中的AOP实验(二)的更多相关文章

  1. Spring中的AOP(二)

    2.5 Spring的织入 在上一篇文章中,我们介绍了Pointcut.Advice.Advisor三个必要模块,剩下的工作就是把它们拼装起来,也就是织入过程.在Spring中,使用类org.spri ...

  2. spring中的AOP 以及各种通知 配置

    理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...

  3. Spring学习笔记(四)—— Spring中的AOP

    一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...

  4. 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)

    Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...

  5. (五)Spring 中的 aop

    目录 文章目录 AOP概念 AOP原理 AOP术语 **`Spring`** 中的 **`aop`** 的操作 使用 `AspectJ` 实现 `aop` 的两种方式 AOP概念 浅理解 aop :面 ...

  6. Spring中的AOP

    什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...

  7. Spring中关于AOP的实践之概念

    一.什么是AOP AOP:也称作面向切面编程 在分享几个概念执行我想先举个栗子(可能例子举得并不是特别恰当): 1.假如路人A走在大街上,被一群坏人绑架了: 2.警察叔叔接到报警迅速展开行动:收集情报 ...

  8. Spring中的AOP 专题

    Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...

  9. spring注解、aop(二)

    使用注解配置spring 1.导入 spring-aop-5.0.6.RELEASE.jar包 2.为主配置文件引入新的命名空间 xmlns:context="http://www.spri ...

随机推荐

  1. PL/SQL变量的作用域和可见性

    变量的作用域和可见性设计变量在块中的位置,不同的位置使得变量具有不同的有效性与可访问性. 变量的作用域是指可以使用变量的程序单元部分,可以是包和子程序包等. 当一个变量在它的作用域中可以用一个不限定的 ...

  2. MyBatis Generator 自定义生成注释

    注释生成器 为了生成db里面的注释,必须自定义注释生成器 EmptyCommentGenerator: import org.mybatis.generator.api.CommentGenerato ...

  3. 【ASP.NET】 HttpContext.Current.User.Identity.Name 返回值为空

    问题起因 在做项目的时候,我使用HttpContext.Current.User.Identity.Name来获取Web应用程序正在使用时的用户名. 在开发过程中,我使用了我的本地iis,启用了集成的 ...

  4. Flutter安装之后cmd运行错误解决

    当把Flutter环境变量配置之后,打开cmd输入:flutter 出现如下错误: 'MySQL' is not recognized as an internal or external comma ...

  5. js 变量作用域

    例子 <script> var a = "heh" function findLove(){ console.log(a); function findforyou() ...

  6. [C#]将数据写入已存在的excel文件

    测试如下(xls/xlsx): //将数据写入已存在Excel public static void writeExcel(string result, string filepath) { //1. ...

  7. 举例说明$POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Mac OS X 操作系统下JDK安装与环境变量配置

    1. 下载JDK. 去oracle官网的Java SE Downloads页面(如图 1),下载Mac os版本JDK(如图 2): 图 1 图 2   2. 安装JDK. 下载完成后,双击.dmg文 ...

  9. mui预加载

    1.只能加载一个页面 mui.init(); var page = null; mui.plusReady(function() { //预加载页面mui.preload必须放在plusReady事件 ...

  10. Fatal error: Uncaught Error: Call to undefined function curl_init()

    系统:win7 对于此错误首先检查php_curl扩展是否开启 , extension=curl  #注意去掉前面的分号 然后检查php\ext下是否有php_curl.dll 文件(默认都有) ph ...