Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
component name="UserService"{
function save(){
log.info("method save() called with arguments: #serializeJSON(arguments)#");
transaction {
// do some work here
}
log.info("Save completed successfully!");
}
}wirebox.listeners = [
{ class="wirebox.system.aop.Mixer", properties={} }
];wirebox.listeners = [
{ class="coldbox.system.aop.Mixer",properties={} }
];component name="UserService"{
function save() transactional{
// do some work here
}
}mapAspect("CFTransaction").to("coldbox.system.aop.aspects.HibernateTransaction");mapAspect("CFTransaction").to("coldbox.system.aop.aspects.CFTransaction");// Map the Aspect
mapAspect("MethodLogger")
.to("coldbox.system.aop.aspects.MethodLogger")
.initArg(name="logResults",value="true or false");
// Bind the Aspect to all service class methods
bindAspect(classes=matcher().regex('.*Service'), methods=matcher().any(), aspects="MethodLogger");
mapAspect("MethodLogger").to("model.aspects.MethodLogger");<cfcomponent output="false" implements="wirebox.system.aop.MethodInterceptor" hint="A simple interceptor that logs method calls and their results">
<--- Dependencies --->
<cfproperty name="log" inject="logbox:logger:{this}">
<--- init --->
<cffunction name="init" output="false" access="public" returntype="any" hint="Constructor">
<cfargument name="logResults" type="boolean" required="false" default="true" hint="Do we log results or not?"/>
<cfscript>
instance = {
logResults = arguments.logResults
};
return this;
</cfscript>
</cffunction>
<--- invokeMethod --->
<cffunction name="invokeMethod" output="false" access="public" returntype="any" hint="Invoke an AOP method invocation">
<cfargument name="invocation" required="true" hint="The method invocation object: wirebox.system.aop.MethodInvocation">
<cfscript>
var refLocal = {};
var debugString = "target: #arguments.invocation.getTargetName()#,method: #arguments.invocation.getMethod()#,arguments:#serializeJSON(arguments.invocation.getArgs())#";
// log incoming call
log.debug(debugString);
// proceed execution
refLocal.results = arguments.invocation.proceed();
// result logging and returns
if( structKeyExists(refLocal,"results") ){
if( instance.logResults ){
log.debug("#debugString#, results:", refLocal.results);
}
return refLocal.results;
}
</cfscript>
</cffunction>
</cfcomponent>/**
* @classMatcher any
* @methodMatcher annotatedWith:transactional
*/
component name="TransactionAspect" implements="coldbox.system.aop.MethodInterceptor"{
function init(){ return this; }
function invokeMethod(required invocation) output=false{
// Let's do the around advice now:
transaction {
// execute the method or other aspects in a transaction
return arguments.invocation.proceed();
}
}
}mapAspect("TransactionAspect").to("model.aspects.MyTransactionAspect");<--- Dependencies --->
<cfproperty name="log" inject="logbox:logger:{this}"><--- init --->
<cffunction name="init" output="false" access="public" returntype="any" hint="Constructor">
<cfargument name="logResults" type="boolean" required="false" default="true" hint="Do we log results or not?"/>
<cfscript>
instance = {
logResults = arguments.logResults
};
return this;
</cfscript>
</cffunction><--- invokeMethod --->
<cffunction name="invokeMethod" output="false" access="public" returntype="any" hint="Invoke an AOP method invocation">
<cfargument name="invocation" required="true" hint="The method invocation object: wirebox.system.aop.MethodInvocation">
<cfscript>
var refLocal = {};
var debugString = "target: #arguments.invocation.getTargetName()#,method: #arguments.invocation.getMethod()#,arguments:#serializeJSON(arguments.invocation.getArgs())#";
// log incoming call
log.debug(debugString);
// proceed execution
refLocal.results = arguments.invocation.proceed();
// result logging and returns
if( structKeyExists(refLocal,"results") ){
if( instance.logResults ){
log.debug("#debugString#, results:", refLocal.results);
}
return refLocal.results;
}
</cfscript>
</cffunction>var refLocal = {};
var debugString = "target: #arguments.invocation.getTargetName()#,method: #arguments.invocation.getMethod()#,arguments:#serializeJSON(arguments.invocation.getArgs())#";
// log incoming call
log.debug(debugString);// proceed execution
refLocal.results = arguments.invocation.proceed();// result logging and returns
if( structKeyExists(refLocal,"results") ){
if( instance.logResults ){
log.debug("#debugString#, results:", refLocal.results);
}
return refLocal.results;
}/**
* @classMatcher any
* @methodMatcher annotatedWith:transactional
*/
OR
component classMatcher="any" methodMatcher="annotatedWith:transactional"{}mapAspect(aspect="TransactionAspect",autoBind=false).to("model.aspects.MyTransactionAspect");
// match only methods that start with the regex ^save
bindAspect(classes=match().any(),methods=match().regex("^save"),aspects="TransactionAspect");bindAspect(classes=match().mappings('UserService'),methods=match().methods('save'),aspects="MethodLogger");component name="UserService"{
function save(){
transaction {
// do some work here
}
}
}
<cfinterface hint="Our AOP Method Interceptor Interface">
<--- invokeMethod --->
<cffunction name="invokeMethod" output="false" access="public" returntype="any" hint="Invoke an AOP method invocation">
<cfargument name="invocation" required="true" hint="The method invocation object: wirebox.system.ioc.aop.MethodInvocation">
</cffunction>
</cfinterface>// ColdBox
coldbox.system.aop
// WireBox Standalone
wirebox.system.aop