4.3. Aspect using S2AopFactory

By the create method of S2AopFactory class, the extended class of an aspect target class is generated and the instance which wove in the aspect is acquired.

S2AopFactory.create(component_class, aspects)
  • The 1st argument : Aspect target class
  • The 2nd argument : Array of Aspects woven in

As an example, an aspect is applied to the following Service class.

module Example
  class Service
    def add(a, b)
      return a + b
    end
  end
end

TraceInterceptor is aspected to add method of Service class as follws.

require 's2container'
require 'seasar/aop/s2aop-factory'
require 'seasar/aop/aspect'
require 'seasar/aop/pointcut'
require 'example'

interceptor = Seasar::Aop::Interceptor::TraceInterceptor.new
pointcut    = Seasar::Aop::Pointcut.new(/^add$/)
aspect      = Seasar::Aop::Aspect.new(interceptor, pointcut)
service     = Seasar::Aop::S2AopFactory::create(Example::Service, [aspect]).new

p service.add(2, 3)

Interceptor. 

Interceptor is a class which implements call method. These next interceptors are attached to S2Aop.

  • Seasar::Aop::Interceptor::TraceInterceptor
    TraceInterceptor is a interceptor for treating trace processing as "Crosscutting Concern".

Pointcut. 

Pointcut is a class showing where Interceptor is applied.

Seasar::Aop::Pointcut.new(target)

Regexp, String or Symbol which specify the method used as Pointcut is passed to a constructor argument.

Aspect. 

Aspect is a class which associates Interceptor(Advice) and Pointcut.

Seasar::Aop::Aspect.new(interceptor, pointcut)

Interceptor and Pointcut are specified by a constructor argument.

[Note]NOTE

This Example is located at "example/example09".



© Copyright The Seasar Foundation and the others 2008-2009, all rights reserved.