软件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. postgresql开启网络连接

    默认情况下,postgresql是只允许localhost连接的,如果需要使用远程连接,需要修改两个配置文件. postgresql.conf 和 pg_hba.conf 在postgresql.co ...

  2. Spring中三种编程式事务的使用

    引入事务管理器 @Autowired TransactionTemplate transactionTemplate; @Autowired PlatformTransactionManager tr ...

  3. rabbitmq作为mqtt服务器实现websocket消息推送给浏览器

    rabbitmq的RabbitMQ Web MQTT插件可以用来支持将rabbitmq作为MQTT协议的服务器,而websocket支持mqtt协议通信实现消息推送.因为我们目前使用rabbitmq, ...

  4. linux常用英文单词记录

    1.skip 跳过忽略 2.next 下一步3.hostname 主机名4.password 密码5.complete 完成6.network 网络7.conf config configuratio ...

  5. [Python数据挖掘]第8章、中医证型关联规则挖掘

    一.背景和挖掘目标 二.分析方法与过程 1.数据获取 2.数据预处理  1.筛选有效问卷(根据表8-6的标准) 共发放1253份问卷,其中有效问卷数为930  2.属性规约 3.数据变换 ''' 聚类 ...

  6. 利用JS模拟排队系统

    我爱撸码,撸码使我感到快乐!大家好,我是Counter.今天给大家分享的是js模拟排队系统,刚开始有排队序列,序列里有vip用户和普通用户,vip用户永远位于普通用户的前面,只有当当前vip用户都办理 ...

  7. win10 开机自启指定软件

    开机自启 %programdata%\Microsoft\Windows\Start Menu\Programs\StartUp

  8. 第一次跑eureka

  9. 意外get接近完美的黑苹果 (UEFI + GPT)

    本人大学生一枚,对于高大上的 MAC OS 只能是摸摸口袋 咽咽口水啦.听说黑苹果,就是安装在普通的 pc 上的 MAC系统,那么对应的苹果电脑上的 MAC OS 系统就为白苹果了. 个人也想啃一口黑 ...

  10. eclipse报错:Multiple annotations found at this line: - String cannot be resolved to a type解决方法实测

    Multiple annotations found at this line:- String cannot be resolved to a type- The method getContext ...