Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 移:Spring AOP的简单例子
【标  题】:移:Spring AOP的简单例子
【关键字】:Spring,AOP
【来  源】:http://blog.csdn.net/kentchenj/archive/2006/09/13/1218006.aspx

移:Spring AOP的简单例子

Your Ad Here Spring AOP的简单例子
一个使用Spring AOP的简单例子

有一个UserLogger类,用于记录user信息:

public class UserLogger {
    private String userName = "";

    public void setUserName(String i) {
        this.userName = i;
    }

    //记录用户信息
    public void log() {
        System.out.println("记录用户:" + this.userName);
    }
}

在定义拦截器advicer,用于用户校验:

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class SecurityAdvice implements MethodBeforeAdvice {

    private SecurityManager securityManager;

    public SecurityAdvice() {
        this.securityManager = new SecurityManager();
    }

    public void before(Method method, Object[] args, Object target) throws Throwable {
        if(method.getName().equals("log")){
            securityManager.check();
        }
    }

}

接着定义校验管理类:

public class SecurityManager {
    //定义成ThreadLocal,因为SecurityAdvice中也new了SecurityManager
    //可以把SecurityManager定义成singleton的就可以替代
    private static ThreadLocal userName = new ThreadLocal();
    private String password;
  
    public void login(String name, String password){
        userName.set(name);
        this.password = password;
    }
  
    public void check() {
        String u = (String)userName.get();
        if (u == null) {
            System.out.println("没有用户");
        } else if ("KENT".equals(u)) {
            System.out.println("Kent登陆成功");
        } else {
            System.out.println("用户:" + u + "没有权限");
        }
    }
}

最后是测试代码:

import org.springframework.aop.framework.ProxyFactory;
public class TestAOP {
    public static void main(String[] args) {
        SecurityManager manager = new SecurityManager();
      
        //设置ProxyFactory的advice(aop方式),target(需要被aop调用的对象。)
        UserLogger target = new UserLogger();
        SecurityAdvice advice = new SecurityAdvice();
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(target);
        proxyFactory.addAdvice(advice);
      
        UserLogger bean = (UserLogger) proxyFactory.getProxy();
      
        String userName = "KENT";
        String password = "";
      
        manager.login(userName, password);//保存了用户信息,未执行check方法
        bean.setUserName(userName);
        bean.log();//自动执行check()方法,AOP调用成功
       
        userName = "TOM";
        manager.login(userName, password);
        bean.setUserName(userName);
        bean.log();
    }
}

上面完成了如下功能:用户登陆,打印Log信息,在打印Log信息前使用AOP进行用户校验
移:使用Spring AOP实现MVC拦截器:【上一篇】
移:AOP技术原理:【下一篇】
【相关文章】
  • 移:使用Spring AOP实现MVC拦截器
  • Spring中实现AOP的基本原理
  • spring+hibernate+hessian动态模型(map)方式lazy=false的问题
  • Spring , Struts整合方法
  • Spring学习笔记
  • Spring从菜鸟到高手(二)AOP的真正实现
  • 浅谈Spring(一)
  • Struts+hibernate+Spring 的结构介绍
  • Spring and OSGi相关信息
  • Spring | Semantics based
  • 【随机文章】
  • AIX简介
  • 设计一个自动编码解码库 (二)
  • 最佳的编程字体
  • 用JavaScript使链接按钮不断变化
  • 坏了坏了,以后用/proc读取Linux系统信息可能要给联想交专利费了
  • 列表显示所有网上邻居
  • 必须掌握的八个DOS命令
  • 我的adsl-server被攻击了
  • McAfee VirusScan Enterprise 8.5i Beta III 更新
  • 常用算法的链接
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.