2009年4月30日星期四

Spring Annotation详解(AOP篇1)

相关文章:Spring Annotation详解(AOP篇2)
Spring Annotation详解(IOC篇)
本文主要简单了解一下使用Java annotation的情况下,先介绍一下比较主流和简单的用法,Spring对AspectJ的支持。
在ApplicationContext中加入关于AOP的命名空间,具体内容见Spring 使用AspectJ来配置AOP
首先明确一点,用作通知(Advice)的类也应该受到ApplicationContext的管理,之后再使用AspectJ的标签来管理通知动作,见代码:
//import ...
@Component
@Aspect
public class Alert {
    @Before("execution(* get*(..))")//方法运行前通知
    public void show() {
        System.out.println("before");
    }
    @After("execution(* get*(..))")//最终通知
    public void show2() {
        System.out.println("after");    
    }
    @Pointcut("execution(* get*(..))")
    public void afterReturn(){}
    //方法返回值通知,注意这个通知需要与@Pointcut配合使用
    @AfterReturning(pointcut="afterReturn()",returning="value")
    public void returning(Object value){
        System.out.println("returning");
    }
    @AfterThrowing(pointcut="afterReturn()",throwing="ex")
    //异常通知,这个通知复用了afterReturn的pointcut。当然你也可以使用新的pointcut
    public void doRecoveryActions(Exception ex){
        //...
    }
    @Around("afterReturn()")
    //环绕通知。真正的方法实际上是joinPoint.proceed();
    public Object doSomeForAround(ProceedingJoinPoint joinPoint) throws Throwable
    {
       System.out.println("before around");
       Object retVal=joinPoint.proceed();
       System.out.println("after around");
      return retVal;
    }
}

这段代码的意思就是在所有以get开头的方法被调用之前和之后都会执行show和returning,show2方法,其运行顺序是show(),returning(),show2()。注意*与get*之间有一个空格。对一个方法,你可以同时使用Before Advice,Around Advice,AfterReturning Advice,After Advice,Throwing Advice。那么这些通知的顺序是什么呢?一个方法运行时,最先触发的是Around Advice,之后是Before Advice 是 AfterReturning(如果有的话),之后是After Advice,最后还要再通知Around。
就拿上面代码这个例子说,打印的顺序是:
before around
before
returning
after
after around

Ruby:Timezone synchronization function(跨时区同步时间方法)

方法是:首先取得客户端的时区,之后得到服务器的本地时区,之后取得两个时区的差值,计算出相应的时间。
输入:服务器时间
输出:相应的客户端对应的时间
上代码:
  #时间同步方法
  #clint_timezone:client timezone
  def formate_date_to_client_time_zone(clint_timezone,time)
    diff=(clint_timezone-get_server_time_zone)
    unless(clint_timezone.nil?)
      time=time+diff*3600
    end
    return time
  end
  #获得本地时区
  #return the server timezone
  def get_server_time_zone
    return Time.now.gmtoff/3600
  end

2009年4月29日星期三

Ruby:string to date Or date to string

Ruby:string to date Or date to string
#string to date:
str="2009-02-02"
date=Date.strptime(str,"%Y-%m-%d")
#date to string:
date=Date.new(2009,2,2)
str=date.strftime("%Y-%m-%d")

2009年4月28日星期二

超炫的flex特效,UI做到这份上,应该说无敌了。

Efflex 是一组由 Stephen Downs (aka “Tink”)开发的 Flex特效。
留个纪念,慢慢看
这里是demo

正则表达式验证邮箱地址

定义以下模式为有效的:
  1. john@hotmail.com
  2. john.doe@somewhere.com
  3. John Doe<john.doe@somewhere.com>
javascript的正则表达式为
var reEmail=/^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;

2009年4月27日星期一

Flex:浅谈Flex事件机制

      一直对RIA技术非常感兴趣,研究了一下Flex的事件机制,发现Flex的事件机制和Javascript很像,只不过更加严格。在Flex的事件机制中有一个比较重要的概念dispatchEvent,基本原理就是事件所在的容器类初始化的时候注册其事件监听函数。如果需要自己定义Event类型,需要将事件分发到事件队列中,也就是dispatchEvent。也就是你要手动找到自定义的事件的前一个事件,在这个结束的时候调用dispatchEvent(event),这个event就是你自定一个事件,这个事件(一个类)一定要继承flash.events.Event
给个例子:
自定义的事件类:
public class AddressFormEvent extends Event

{
public static const SUBMIT:String = "submit";
private var _data:AddressVO;
public function AddressFormEvent (eventName:String)
{
super (eventName);
}
public function set data (value:AddressVO):void
{
_data = value;
}
public function get data ():AddressVO
{
return _data;
}
}
在上一个事件结束的时候发布这个事件,下面这段代码就是找到上一个事件:
public class AddressFormClass extends Form

{
public var submitButton:Button;
public var nameInput:TextInput;
public var street:TextInput;
public var city:TextInput;
public var state:TextInput;
public var country:CountryComboBox;
public var otherInput:TextInput;
public var otherText:Text;
public function AddressFormClass():void
{
addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler);
}
private function creationCompleteHandler(event:FlexEvent):void
{
submitButton.addEventListener(MouseEvent.CLICK,submitHandler);
}
private function submitHandler(event:MouseEvent):void //这个就是上一个事件的监听函数
{
// Gather the data for this form
var addressVO:AddressVO = new AddressVO();
addressVO.name = nameInput.text;
addressVO.street = street.text;
addressVO.city = city.text;
addressVO.state = state.text;
addressVO.country = country.selectedItem as String;
// 下面就是创建这个事件自定义事件,注意类型就是我们刚刚建的那个事件类。
var submitEvent:AddressFormEvent = new AddressFormEvent(AddressFormEvent.SUBMIT);
submitEvent.data = addressVO;
// Dispatch an event to signal that the form has been submitted
dispatchEvent(submitEvent); //这就是发布这个时间
}
}

2009年4月26日星期日

Spring 使用AspectJ来配置AOP

在Application_context中引入名称空间,例子:<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/lang
     http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  <context:component-scan base-package="bean"/>
  <aop:aspectj-autoproxy/>
</beans>