LoggingAOP with ColdSpring
今日は、ColdSpringAOPとLog4jを使ったロギングサービスについて、解説します。
Chris ScottのColdSpringAOPのチュートリアルから引用してみます。
1.最初に、LoggingServiceを定義します。javaのlog4jからloggerクラスを生成しています。
<cfcomponent name="LoggingService" output="false">
<cffunction name="init" returntype="net.klondike.service.LoggingService" access="public" output="false">
<cfset var category = CreateObject("java", "org.apache.log4j.Category") />
<cfset variables.logger = category.getInstance('net.klondike') />
<cfreturn this />
</cffunction>
<cffunction name="info" access="public" returntype="void" output="false">
<cfargument name=message" type="string" required="true" />
<cfif variables.logger.isInfoEnabled()>
<cfset variables.logger.info(arguments.message) />
</cfif>
</cffunction>
</cfcomponent>
2.次に、AOPパートです。loggingBeforeAdviceを定義します。
<cfcomponent name="loggingBeforeAdvice" extends="coldspring.aop.BeforeAdvice">
<!--- setters for dependencies --->
<cffunction name="setLoggingService" returntype="void" access="public""false" hint="Dependency: security service"> output=
<cfargument name="loggingService" type="net.klondike.service.LoggingService" required="true"/>
<cfset variables.m_loggingService = arguments.loggingService />
</cffunction>
<cffunction name="getLoggingService" returntype="net.klondike.service.LoggingService""public" output="false" hint="Dependency: security service"> access=
<cfreturn variables.m_loggingService />
</cffunction>
<!--- before() is called by the aop framework before method invocation --->
<cffunction name="before" access="public" returntype="any">
<cfargument name="method" type="coldspring.aop.Method" required="true" />
<cfargument name="args" type="struct" required="true" />
<cfargument name="target" type="any" required="true" />
<cfset var arg = '' />
<cfset var argString = '' />
<cfset var objName = getMetadata(arguments.target).name />
<cfloop collection="#args#" item="arg">
<cfif isSimpleValue(args[arg])>
<cfif len(argString)>
<cfset argString = argString & ', ' />
</cfif>
<cfset argString = argString & arg & '=' & args[arg] >
</cfif>
</cfloop>
<cfset variables.m_loggingService.info("[" & objName & "] " & method.getMethodName() & "(" & argString & ") called!") />
</cffunction>
</cfcomponent>
3.最後に、ColdSpringのbean定義を行います。
<beans>
<!—define the logging service -->
<bean id="loggingService"
class="net.klondike.component.LoggingService" />
<!--define the loggingBeforeAdvice and set its logging service property to reference the bean above -->
<bean id="loggingBeforeAdvice"
class="net.klondike.aspects.loggingBeforeAdvice">
<property name="loggingService">
<ref bean="loggingService" />
</property>
</bean>
<!—now define a NamedMethodPointcutAdvisor, set the advice property to the bean above, and set its mappedNames property to ‘*’ which will create a pointcut to match all methods excluding any init method -->
<bean id="loggingAdvisor"
class="coldspring.aop.support.NamedMethodPointcutAdvisor">
<property name="advice">
<ref bean="loggingBeforeAdvice" />
</property>
<property name="mappedNames">
<value>*</value>
</property>
</bean>
<!-- to create the proxy object, first create our dao as the target object -->
<bean id="catalogDaoTarget"
class="net.klondike.component.catalogDAO">
<property name="dsn">
<value>klondike</value>
</property>
</bean>
<!-- now create a ProxyFactoryBean with the id catalogDAO, set the target to the catalogDaoTarget bean above, and give it ,the id of the NamedMethodPointcutAdvisor above in the list of interceptorNames -->
<bean id="catalogDAO"
class="coldspring.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="catalogDaoTarget" />
</property>
<property name="interceptorNames">
<list>
<value>loggingAdvisor</value>
</list>
</property>
</bean>
<!--now to use the proxy DAO, we give the id of the ProxyFactoryBean to the catalog service as the reference for the property catalogDAO -->
<bean id="catalogService"
class="net.klondike.component.CatalogService">
<property name="catalogDAO">
<ref bean="catalogDAO" />
</property>
</bean>
</beans>
コメント