使用Spring自动生成代理类,spring利用的是动态代理机制 
接口

Java代码 
public interface UserDao { 
void addUser(); 
void deleteUser(); 

两个实现:

Java代码 
public class UserDaoMySqlImpl implements UserDao {   
  
@Override  
public void addUser() {   
  System.out.println("UserDaoMySqlImpl.addUser()");   
}   
  
@Override  
public void deleteUser() {   
  System.out.println("UserDaoMySqlImpl.deleteUser()");   
  
}   
  
}

public class UserDaoOracleImpl implements UserDao {

@Override 
public void addUser() { 
  System.out.println("UserDaoOracleImpl.addUser()"); 
}

@Override 
public void deleteUser() { 
  System.out.println("UserDaoOracleImpl.deleteUser()"); 
}


Device 
import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class UserDaoBeforeAdvice implements MethodBeforeAdvice {

@Override 
public void before(Method arg0, Object[] arg1, Object arg2) 
   throws Throwable { 
  System.out.println("UserDaoBeforeAdvice.before()"); 
}

}

spring的配置文件

Xml代码 
<!-- 配置实现类的bean,可以把所有的实现类都写上 -->  
<bean id="userdao" class="com.dowebber.spring.aop.UserDaoMySqlImpl"></bean>  
<!--配置Advice的bean -->  
<bean id="beforeadvice" class="com.dowebber.spring.aop.UserDaoBeforeAdvice"></bean>  
<!--配置spring自动生成代理对象所需要的一些参数 -->  
<bean id="userdaoproxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
<property name="proxyInterfaces" value="com.dowebber.spring.aop.UserDao"/>  
<property name="target">  
  <ref bean="userdao"/>  
</property>  
<property name="interceptorNames">  
  <list>  
   <value>beforeadvice</value>  
  </list>  
</property>  
</bean>

测试类:

Java代码 
import org.springframework.context.ApplicationContext;   
import org.springframework.context.support.ClassPathXmlApplicationContext;   
  
public class TestUserDaoBeforeDevice {   
  
public static void main(String[] args) {   
     
  ApplicationContext context = new ClassPathXmlApplicationContext("com/dowebber/spring/aop/spring.xml");   
  UserDao dao = (UserDao)context.getBean("userdaoproxy");   
  dao.addUser();   
}   
  
}

AOP的MethodBeforeAdvice的更多相关文章

  1. Spring5最新完整教程IDEA版【通俗易懂2019.11月】

    1.Maven找包: spring-webmvc spring-jdbc 2.Spring的本质是控制反转,依靠依赖注入来实现.以一个servcie对象为例,即是service暴露注入接口(构造,se ...

  2. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  3. spring aop

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...

  4. Spring的IOC和AOP之深剖

    今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...

  5. AOP详解

    什么是AOP AOP Aspect Oriented Programing 面向切面编程 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查.缓存) Spring ...

  6. Spring中Aop的扩展及剖析

    AOP简介: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范 ...

  7. AOP面向切面编程的四种实现

     一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...

  8. spring笔记--使用springAPI以及自定义类 实现AOP的一个例子

    Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...

  9. Spring实现AOP的4种方式

    了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...

随机推荐

  1. 【2018.06.26NOIP模拟】T2号码bachelor 【数位DP】*

    [2018.06.26NOIP模拟]T2号码bachelor 题目描述 Mike 正在在忙碌地发着各种各样的的短信.旁边的同学 Tom 注意到,Mike 发出短信的接收方手机号码似乎都满足着特别的性质 ...

  2. 语义版本号(Semantic Versioning)

    版本号格式不陌生吧,.NET 传统的版本号格式类似这样 1.5.1254.0.本文将推荐一种新的版本号格式——语义版本号,格式类似这样 1.4.6-beta.我推荐语义版本号是因为这样的版本号自包含语 ...

  3. bzoj2748(HAOI2018)音量调节

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2748 水得不想写.别忘了-1. #include<iostream> #incl ...

  4. 【常见Web应用安全问题】---4、Directory traversal

    Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些.  常见Web应用安全问题安全性问题的列表: 1.跨站脚本攻击(CSS or XSS, Cross Si ...

  5. ( 转)mappingResource属性和mappingDirectoryLocations属性的使用

    在Spring的applicationContext.xml中配置映射文件,通常是在这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionFactory的所属类LocalSession ...

  6. Linq 分组(group by)后列变行

    表一: 表二: 已知表一的List,想得到表二的结果: var query = from c in t.AsEnumerable() group c by new { pingming = c.Fie ...

  7. jeecg中选择的数据字典

    <t:dictSelect field="fjingji" hasLabel="false" typeGroupCode="fjingji&qu ...

  8. 解决Oracle的http://localhost:1158/em页面打不开的问题

    https://localhost:1158/em 无法显示页面,在网上查阅资料以后发现这个页面时由服务:OracleDBConsoleoracl控制的,所以到管理界面打开服务:OracleDBCon ...

  9. Unit05: WEB项目的开发模式 、转发 和 Unit09: EL、JSTL

    Unit05: WEB项目的开发模式 .转发   和  Unit09: EL.JSTL dao package dao; import java.io.Serializable; import jav ...

  10. GOF23设计模式之适配器模式(Adapter)

    一.适配器模式概述 将一个类的接口转换成客户可用的另外一个接口. 将原本不兼容不能在一起工作的类添加适配处理类,使其可以在一起工作. 二.适配器模式场景 要想只有USB接口的电脑想使用PS/2接口的键 ...