spring中的AOP实验(二)
软件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实验(二)的更多相关文章
- Spring中的AOP(二)
2.5 Spring的织入 在上一篇文章中,我们介绍了Pointcut.Advice.Advisor三个必要模块,剩下的工作就是把它们拼装起来,也就是织入过程.在Spring中,使用类org.spri ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
- (五)Spring 中的 aop
目录 文章目录 AOP概念 AOP原理 AOP术语 **`Spring`** 中的 **`aop`** 的操作 使用 `AspectJ` 实现 `aop` 的两种方式 AOP概念 浅理解 aop :面 ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Spring中关于AOP的实践之概念
一.什么是AOP AOP:也称作面向切面编程 在分享几个概念执行我想先举个栗子(可能例子举得并不是特别恰当): 1.假如路人A走在大街上,被一群坏人绑架了: 2.警察叔叔接到报警迅速展开行动:收集情报 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- spring注解、aop(二)
使用注解配置spring 1.导入 spring-aop-5.0.6.RELEASE.jar包 2.为主配置文件引入新的命名空间 xmlns:context="http://www.spri ...
随机推荐
- zentao安装升级
------------------------一键安装8.4.1版本-------------------------------------1.查看操作系统版本[root@kvm-180 soft ...
- mysql配置外部允许外部连接
1. 登录到mysql mysql -u root -p 2.进入到mysql 库中 use mysql 3.执行语句 update user set host=‘%’ where user=‘roo ...
- Python3 tkinter基础 Text window 文本框中插入按钮
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- list,tuple,dict,set的增删改查
数据结构 list tuple dict set 增 append insert d['key']=value add 删 pop pop(0) d.pop('name') pop re ...
- SpringBoot后台接收前台的字符串数据
需求 将前台传入的字符串数据转为int类型. 操作 在pom.xml中添加引用. <dependency> <groupId>org.apache.commons</gr ...
- python笔记--socket编程
socket编程 osi七层模型 socket Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族 ...
- Python+MapReduce实现矩阵相乘
算法原理 map阶段 在map阶段,需要做的是进行数据准备.把来自矩阵A的元素aij,标识成p条<key, value>的形式,key="i,k",(其中k=1,2,. ...
- Win10提示“因为文件共享不安全,所以你不能连接到文件共享”如何处理
在使用Windows10 1803版本系统连接CentOS6.5下搭建的Samba服务时,发现打开共享文件会遇到以下提示: 其实,该问题是Win10版本不兼容导致的.微软官方说明:https://go ...
- leecode第二百一十七题(存在重复元素)
class Solution { public: bool containsDuplicate(vector<int>& nums) { set<int> s; for ...
- Django 管理站点
这一部分是关于 Django 的自动管理界面.这个特性是这样起作用的:它读取你模式中的元数据,然后提供给你一个强大而且可以使用的界面,网站管理者可以用它立即工作.在这里我们将讨论如何激活,使用和定制这 ...