`
jiaguwen123
  • 浏览: 404910 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

AOP的MethodBeforeAdvice

阅读更多
使用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();  
}  
 

分享到:
评论

相关推荐

    spring_AOP的MethodBeforeAdvice详解

    spring_AOP的MethodBeforeAdvice详

    spring aop spring aop

    spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop

    Spring  AOP实现方法大全

    顾名思义,Before Advice会在目标对象的方法执行之前被调用,您可以通过实现org.springframework.aop.MethodBeforeAdvice接口来实现Before Advice的逻辑,接口定义如下: java 代码 1. package org.springframework....

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 aopalliance-1.0

    开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...

    spring aop实现

    spring,aop的实现.MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor等。

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    反射实现 AOP 动态代理模式(Spring AOP 的实现原理)

    AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理

    基于注解实现SpringAop

    基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop

    aop所依赖的所有包

    aop所依赖的所有包+文档+源码,最新版全套aop aspectjweaver aopalliance aspects aspectjrt

    spring aop jar 包

    spring aop jar 包

    spring中AOP中标签加载通知

    spring中AOP中标签加载通知,spring中AOP中标签加载通知spring中AOP中标签加载通知spring中AOP中标签加载通知,spring中AOP中标签加载通知

    spring aop 自定义注解保存操作日志到mysql数据库 源码

    3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...

    android aop

    自己写的android aop的例子 可以说明在android开发中如何使用aop编码的

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring-boot aop

    源代码是spring-boot添加aop切面功能的demo,简单易懂.

    Spring aop 之 静态代理 动态代理 Aspectj aop-config 等实现方式

    主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...

    AOP使用CGLIB实现AOP功能

    Spring AOP实现方法之一:CGLIB 实现AOP功能

    aop例子aop例子

    自己写的aop代理spring

    Spring AOP 16道面试题及答案.docx

    描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...

Global site tag (gtag.js) - Google Analytics